我的休眠项目有以下设计:
@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;
}
}
如您所见,用户与配置文件之间存在多对多关系。
@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;
}
}
如果我现在尝试创建一名员工,我会收到一个休眠异常:
org.hibernate.AnnotationException:使用 @OneToMany 或 @ManyToMany 定位 未映射的类: de.ke.objects.bo.profile.Profil.user[de.ke.objects.bo.user.User]
如何在超类
User
上使用多对多关系进行分析,以便它适用于 Client
和 Employee
?
问题在于对 User 的引用,它不是一个实体。一个外键只能引用一张表。使用@ManyToAny(可以处理多个表)或@Inheritance(strategy=InheritanceType.SINGLE_TABLE)(它将所有实体放入一张表中)。
这里是关于hibernate继承的文档:http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168
我也遇到了这个问题,我很想知道你如何解决这个问题而不在数据库中创建用户表