jpa - EclipseLink 中的多个可写映射异常

标签 jpa orm ejb jpa-2.0 eclipselink

我有这些表:

tables

我的意图是:用户可以是公司个人,但他们每个人都有一些共同点,例如用户名,即电子邮件password,因此我使用 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
}

最佳答案

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

关于jpa - EclipseLink 中的多个可写映射异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7952115/

相关文章:

java - 在 EL 中访问无状态 session bean 的简单方法?

java - TomEE 中使用 EJB 3.1 的推土机

java - 可以用独立的 JPA 测试 EJB3/JPA 吗?

java - 建模 "specific products"数据库

mysql - JPQL jpa (@Query) for update column with binary operator mysql query 是什么?

hibernate - 如何知道/记录查询是否使用了 Hibernate 二级缓存?

java - 了解如何在 hibernate 中的急切(非惰性)加载中获取对象

postgresql - JPA 和 Postgres 序列预分配大小设置不正确

java - 有什么方法可以像这样使用 JPA2 标准 API 进行 Join 吗?

hibernate - jOOQ 无法映射 PostgreSQL 列以使 JPA 满意