EclipseLink中的多个可写映射异常

时间:2011-10-31 09:55:16

标签: jpa orm ejb jpa-2.0 eclipselink

我有这些表格:

tables

我的意图是:用户可以是companyperson,但每个用户都有一些共同点,即email和{{1}的用户名因此,我使用JPA工具从表中生成实体,结果如下:

password

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

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

}

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

更新

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: 

3 个答案:

答案 0 :(得分:35)

执行此操作的正确方法是使用@PrimaryKeyJoinColumn而不是普通的@JoinColumn

reference wiki example on PrimaryKeyJoinColumn

答案 1 :(得分:32)

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

答案 2 :(得分:4)

我的猜测是你有两次id_user映射,一次使用Basic @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