use*_*409 5 java annotations hibernate
我的hibernate项目有以下设计:
@MappedSuperclass
public abstract class User {
private List<Profil> profile;
@ManyToMany (targetEntity=Profil.class)
public List<Profil> getProfile(){
return profile;
}
public void setProfile(List<Profil> profile) {
this.profile = profile;
}
}
@Entity
@Table(name="client")
public class Client extends User {
private Date birthdate;
@Column(name="birthdate")
@Temporal(TemporalType.TIMESTAMP)
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate= birthdate;
}
}
@Entity
@Table(name="employee")
public class Employee extends User {
private Date startdate;
@Column(name="startdate")
@Temporal(TemporalType.TIMESTAMP)
public Date getStartdate() {
return startdate;
}
public void setStartdate(Date startdate) {
this.startdate= startdate;
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,用户可以看到与Profile的ManyToMany关系.
@Entity
@Table(name="profil")
public class Profil extends GObject {
private List<User> user;
@ManyToMany(mappedBy = "profile", targetEntity = User.class )
public List<User> getUser(){
return user;
}
public void setUser(List<User> user){
this.user = user;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我现在尝试创建一个员工,我会得到一个hibernate异常:
org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:de.ke.objects.bo.profile.Profil.user [de.ke.objects.bo.user.User]
如何在超类上使用ManyToMany-Relationship配置文件User,因此适用于Client和Employee?
问题在于对 User 的引用,它不是一个实体。一个外键只能引用一张表。使用@ManyToAny(可以处理多个表)或@Inheritance(strategy=InheritanceType.SINGLE_TABLE)(它将所有实体放入一张表中)。
这是有关休眠中继承的文档:http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168
| 归档时间: |
|
| 查看次数: |
3888 次 |
| 最近记录: |