我有这些表:
这我的意图是:用户可以是一个company
或person
,但他们每个人都有一个共同点,如用户名这是email
和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
}
现在,如果我尝试坚持的用户对象launchs中的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
}
要做到这一点,正确的方法是使用,而不是普通的老@PrimaryKeyJoinColumn
@JoinColumn
。
我解决我的问题放置insertable=false, updatable=false
在@JoinColumn
注释中的两个类,个人和公司。
我的猜测是,你有id_user映射两次,一次使用基本@Id映射,而一旦使用@ManyToOne。你需要让他们只读之一,即插入/更新=假。或者更好的,只是删除了基本的ID,并把@Id上@ManyToOne。