Hibernate映射的超类关系和覆盖 [英] Hibernate Mapped Superclass relationships and overriding
问题描述
我有一个抽象的MappedSuperClass参与者,它由三种'参与者'扩展。然后每个人都使用自己的'项目',也是一个抽象的MappedSuperClass。但是,我希望基类知道项目,以便我可以编写通用代码来与参与者交互。我如何使用Hibernate注释来指定它?以及如何在ExtendedParticipant和ExtendedProject类中覆盖它?
每个参与者类型和每个项目类型都有其自己的数据库表,其中包含现有的数据和ID(不是唯一的我不能改变。
下面的代码给了我IDE错误多对一的属性不应该是'映射超类'。 b
$ b
@MappedSuperclass
公共抽象类Participant实现了Persistable {
...
@ManyToOne
@JoinColumn(name =project_id)
public Project getProject(){
return project;
}
public void setProject(Project project){
this.project = project;
}
...
}
并且Project类与相同的问题大致相同:
@MappedSuperclass
public abstract class Project implements Persistable {
...
@OneToMany
公共列表< Participant> getParticipants(){
返回参与者;
}
public void setProject(List< Participant> participants){
this.participants = participants;
}
...
}
映射超类不是实体,它不能是关联的一部分。因此,将您的类映射为实体,并在其之上引入一个映射超类,或者使用 TABLE_PER_CLASS
策略。 另见
I have an abstract MappedSuperClass, Participant, which is extended by three kinds of 'Participant'. Each one then uses its own kind of 'Project', also an abstract MappedSuperClass. However, I want the base class to know about Projects so I can write generic code to interact with Participants. How do I specify this using Hibernate annotations? and how will I override it in the ExtendedParticipant and ExtendedProject classes?
Each Participant type, and each Project type, have their own database tables with existing data and ids (not unique across tables) that I cannot change.
The following code gives me the IDE error "Many to one attribute should not be 'Mapped Superclass'".
@MappedSuperclass
public abstract class Participant implements Persistable {
...
@ManyToOne
@JoinColumn(name = "project_id")
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
...
}
and the Project class is much the same with the same problem:
@MappedSuperclass
public abstract class Project implements Persistable {
...
@OneToMany
public List<Participant> getParticipants() {
return participants;
}
public void setProject(List<Participant> participants) {
this.participants = participants;
}
...
}
A mapped superclass is not an Entity, it can't be part of an association. So map your classes as entities and either introduce a mapped superclass "above" them or use a TABLE_PER_CLASS
strategy.
See also
这篇关于Hibernate映射的超类关系和覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!