java Hibernate:在扩展另一个注释为 JoinedSubclass 的类的类上使用了mappedBy?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/962767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: Used mappedBy on class that extends another class annotated as JoinedSubclass?
提问by GreenieMeanie
The following doesn't work:
以下不起作用:
@Entity
class Owner {
@OneToMany(mappedBy="owner", cascade = {CascadeType.ALL})
protected Set<B> getBSet() {
..
}
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
class A {
@ManyToOne
public Owner getOwner() {
...
}
}
@Entity
class B extends A {
}
It causes an exception as such: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: B.user in Owner.
它会导致如下异常:org.hibernate.AnnotationException:mappedBy 引用了一个未知的目标实体属性:B.user in Owner。
I am trying to avoid copying the "owner" property into class B (which will consequently "denormalize" and copy the owner key into both tables generated for entity A and B). Also, I would really like to have A and B in a separate table and not have to use a discriminator by using SingleTable inheritance.
我试图避免将“所有者”属性复制到 B 类(这将因此“非规范化”并将所有者密钥复制到为实体 A 和 B 生成的两个表中)。另外,我真的很想将 A 和 B 放在一个单独的表中,而不必通过使用 SingleTable 继承来使用鉴别器。
Also, I can't figure out how to do something similar by using @OneToOne between A and B (and not having B extend A).
另外,我无法弄清楚如何通过在 A 和 B 之间使用 @OneToOne(而不是让 B 扩展 A)来做类似的事情。
回答by Chris Wong
回答by Trevor Allred
Try adding targetEntity = Transaction.class. This worked for me when I was using SINGLE_TABLE inheritance. I didn't try it with JOIN.
尝试添加 targetEntity = Transaction.class。当我使用 SINGLE_TABLE 继承时,这对我有用。我没有用 JOIN 尝试过。
@Entity
class Owner {
@OneToMany(mappedBy="owner", cascade = {CascadeType.ALL}, targetEntity = Transaction.class)
@Where(clause = "tableType='I'")
protected Set<B> getBSet() {
..
}
}
回答by danieljimenez
I'd double check your real implementation. I used your sample code and after adding an @Id everything worked as expected. Even IntelliJ says getBSet() is associated with B.owner.
我会仔细检查您的实际实施情况。我使用了您的示例代码,并在添加 @Id 后一切正常。甚至 IntelliJ 也说 getBSet() 与 B.owner 相关联。

