EclipseLink中的多个可写映射异常

31

我有以下这些表:

tables

我的意图是: 用户可以是一个公司或一个,但他们都有一些共同之处,比如用户名是电子邮件密码,因此我使用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

}

现在,如果我尝试持久化一个用户对象,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: 

生成的映射是否有误?我该如何解决这个异常?

更新

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
}
3个回答

42

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


40

我有一个只有一列的表,该列是另一个表中ID的子集。这对我解决了问题。 - Mr_and_Mrs_D
大家好! 我需要帮助。 我的JSF应用程序在客户(ID)和评论(CUSTID)表之间使用了一对多的关系。 我将@JoinColumn(name="ID", referencedColumnName="CUSTID")切换为@PrimaryKeyJoinColumn(name="ID", referencedColumnName="CUSTID") - Mikro Koder

4

因为提供了参考资料,所以我点了赞。我建议你在回答中也提到 '@PrimaryKeyJoinColumn'。 - S. Pauk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接