Hibernate:与其自身的亲子关系
我有两个表:
表名称:TABLE_A
A_ID
A_CODE
A_DESC
表名称:TABLE_B
B_ID
B_TABLE_A_PARENT_ID
B_TABLE_A_CHILD_ID
其中: TABLE_A 的 A_ID 可以输入到 TABLE_B 的 B_TABLE_A_PARENT_ID 和 B_TABLE_A_CHILD_ID 中以创建与其自身的关系。
我的代码:
@Entity
@Table(name = "TABLE_A")
public class TableA{
private int id;
private String code;
private String desc;
private Set<TableB> tableBSet= new HashSet<TableB>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tableA", cascade = CascadeType.ALL)
public Set<TableB> getTableBSet() {
return tableBSet;
}
public void setTableBSet(Set<TableBSet> tableBSet) {
this.tableBSet = tableBSet;
}
}
在另一个类上:
@Entity
@Table(name = "TABLE_B")
public class TableB{
private TableB_Id id;
private TableA parentA;
private TableA childA;
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "parentTableId", column = @Column(name = "B_TABLE_A_PARENT_ID", nullable = false, precision = 22, scale = 0)),
@AttributeOverride(name = "childTableId", column = @Column(name = "B_TABLE_A_CHILD_ID", nullable = false, precision = 22, scale = 0)) })
public TableB_id getId() {
return this.id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_TABLE_A_PARENT_ID", nullable = false, insertable = false, updatable = false)
public TableA getParentA() {
return this.parentTable;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_TABLE_A_CHILD_ID", nullable = false, insertable = false, updatable = false)
public TableA getChildA() {
return this.childA;
}
}
在 ID 类上:
@Embeddable
public class TableB_Id {
private int parentTableId;
private int childTableId;
@Column(name = "B_TABLE_A_PARENT_ID", nullable = false, precision = 22, scale = 0)
public Integer getParentTableId() {
return this.parentTableId;
}
@Column(name = "B_TABLE_A_CHILD_ID", nullable = false, precision = 22, scale = 0)
public Integer getChildTableId() {
return this.childTableId;
}
}
当我运行服务器时,出现以下错误:
原因为:org.hibernate.AnnotationException:mappedBy 引用未知的目标实体属性:com.parentchild.TableB.tableA in com. Parentchild.TableA.tableB
我认为有问题的代码是上面 TableA 中的第一段代码,但我不知道该怎么做。请善意地帮助我。
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tableA", cascade = CascadeType.ALL)
public Set<TableB> getTableBSet() {
return tableBSet;
}
先感谢您!
I have two tables:
TABLE NAME: TABLE_A
A_ID
A_CODE
A_DESC
TABLE NAME: TABLE_B
B_ID
B_TABLE_A_PARENT_ID
B_TABLE_A_CHILD_ID
Where:
The TABLE_A's A_ID can be entered in TABLE_B's B_TABLE_A_PARENT_ID and B_TABLE_A_CHILD_ID to create relationship to itself.
My Code:
@Entity
@Table(name = "TABLE_A")
public class TableA{
private int id;
private String code;
private String desc;
private Set<TableB> tableBSet= new HashSet<TableB>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tableA", cascade = CascadeType.ALL)
public Set<TableB> getTableBSet() {
return tableBSet;
}
public void setTableBSet(Set<TableBSet> tableBSet) {
this.tableBSet = tableBSet;
}
}
On another class:
@Entity
@Table(name = "TABLE_B")
public class TableB{
private TableB_Id id;
private TableA parentA;
private TableA childA;
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "parentTableId", column = @Column(name = "B_TABLE_A_PARENT_ID", nullable = false, precision = 22, scale = 0)),
@AttributeOverride(name = "childTableId", column = @Column(name = "B_TABLE_A_CHILD_ID", nullable = false, precision = 22, scale = 0)) })
public TableB_id getId() {
return this.id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_TABLE_A_PARENT_ID", nullable = false, insertable = false, updatable = false)
public TableA getParentA() {
return this.parentTable;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "B_TABLE_A_CHILD_ID", nullable = false, insertable = false, updatable = false)
public TableA getChildA() {
return this.childA;
}
}
On the ID class:
@Embeddable
public class TableB_Id {
private int parentTableId;
private int childTableId;
@Column(name = "B_TABLE_A_PARENT_ID", nullable = false, precision = 22, scale = 0)
public Integer getParentTableId() {
return this.parentTableId;
}
@Column(name = "B_TABLE_A_CHILD_ID", nullable = false, precision = 22, scale = 0)
public Integer getChildTableId() {
return this.childTableId;
}
}
When I run the server I get the following error:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.parentchild.TableB.tableA in com.parentchild.TableA.tableB
I think the offending code is the first block of code above in TableA but I don't know what to do. Please help kindly me.
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tableA", cascade = CascadeType.ALL)
public Set<TableB> getTableBSet() {
return tableBSet;
}
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最近,我用复合主键映射了
@Many-To-Many
:您可以轻松地将其更改为@One-To-Many
。这里是完整的代码和解释:
使用复合主键和注释映射多对多:
Recently, I mapped
@Many-To-Many
with composite primary key: you can easily change it to@One-To-Many
.here is complete code and explainations:
Mapping ManyToMany with composite Primary key and Annotation:
该错误消息的原因是没有名为“tableA”的持久属性。 mappedBy 的值应该是持久属性。因此,将其更改为任何应该是反面的内容。也许你希望它是“parentId”或“childId”,我不知道,不能两者都是。
那么您仍然会遇到以下问题:
Reason for that error message is that there is no persistent property named "tableA". Value of mappedBy is supposed to be persistent property. So change it to whatever is supposed to be inverse side. Maybe you want it to be "parentId" or "childId", I don't know, cannot be both.
Then you will still have for example following problems: