EclipseLink中的多个可写映射异常

Val*_*lva 26 orm ejb jpa eclipselink jpa-2.0

我有这些表:

表

我的意图是:用户可以是a company或a person但是每个人都有一些共同点,作为用户名是emailpassword,所以我使用JPA工具从表中生成实体,结果如下:

public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private String email;

    private String password;

    private int reputation;

    //bi-directional one-to-one association to Company
    @OneToOne(mappedBy="user", cascade={CascadeType.ALL})
    private Company company;

    //bi-directional many-to-one association to Location
    @OneToMany(mappedBy="user")
    private List<Location> locations;

    //bi-directional one-to-one association to Person
    @OneToOne(mappedBy="user")
    private Person person;

    //bi-directional many-to-one association to Product
    @OneToMany(mappedBy="user")
    private List<Product> products;

    //bi-directional many-to-one association to UserType
    @ManyToOne
    @JoinColumn(name="type")
    private UserType userType;

    //bi-directional many-to-one association to UserPhone
    @OneToMany(mappedBy="user")
    private List<UserPhone> userPhones;

    //bi-directional many-to-one association to UserPicture
    @OneToMany(mappedBy="user")
    private List<UserPicture> userPictures;

    //bi-directional many-to-one association to UserSocialNetwork
    @OneToMany(mappedBy="user")
    private List<UserSocialNetwork> userSocialNetworks;

        // getter and setters

}
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试持久化用户对象,则在EclipseLink中启动以下异常:

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [COMPANY.id_user].  Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[user]
Descriptor: RelationalDescriptor(entity.Company --> [DatabaseTable(COMPANY)])
Exception [EclipseLink-48] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [PERSON.id_user].  Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[user]
Descriptor: RelationalDescriptor(entity.Person --> [DatabaseTable(PERSON)])
Runtime Exceptions: 
Run Code Online (Sandbox Code Playgroud)

生成的映射是错误的吗?我怎样才能解决这个异常?

更新

public class Company implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id_user")
    private int idUser;

    private String email;

    private String name;

    //bi-directional many-to-one association to Area
    @ManyToOne
    @JoinColumn(name="area")
    private Area areaBean;

    //bi-directional one-to-one association to User
    @OneToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="id_user", insertable=false, updatable=false)
    private User user;

        // getter and setters
}

@Entity
public class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id_user")
    private int idUser;

    @Temporal( TemporalType.DATE)
    private Date birthdate;

    private String gender;

    private String name;

    private String surname;

    //bi-directional one-to-one association to User
    @OneToOne
    @JoinColumn(name="id_user", insertable=false, updatable=false)
    private User user;

        // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

klo*_*onq 37

这样做的正确方法是使用@PrimaryKeyJoinColumn而不是普通的旧@JoinColumn.

在PrimaryKeyJoinColumn上引用wiki示例


Val*_*lva 35

我解决了将问题放在PersonCompany两个类insertable=false, updatable=false中的问题.@JoinColumn


Jam*_*mes 4

我的猜测是,您将 id_user 映射了两次,一次使用基本 @Id 映射,一次使用 @ManyToOne。您需要将其中之一设置为只读,即 insertable/updatable=false。或者更好的是,只需删除基本 id,并将 @Id 放在 @ManyToOne 上。

请参阅 http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships