我的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;
}
}
正如您所看到的,用户可以看到与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;
}
}
如果我现在尝试创建一个员工,我会得到一个hibernate异常:
org.hibernate.AnnotationException:使用 @OneToMany或@ManyToMany定位 未映射的类: de.ke.objects.bo.profile.Profil.user [de.ke.objects.bo.user.User]
如何在超类User
上使用ManyToMany-Relationship配置文件,因此适用于Client
和Employee
?
答案 0 :(得分:3)
问题在于对User的引用,而User不是实体。外键只能引用一个表。可以使用可以处理多个表的@ManyToAny,也可以使用@Inheritance(strategy = InheritanceType.SINGLE_TABLE),它将所有实体放在一个表上。
以下是hibernate中继承的文档:http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168