Hibernate:与其自身的亲子关系

发布于 2024-12-07 14:58:15 字数 2479 浏览 1 评论 0原文

我有两个表:

表名称: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素手挽清风 2024-12-14 14:58:15

最近,我用复合主键映射了@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:

第七度阳光i 2024-12-14 14:58:15

该错误消息的原因是没有名为“tableA”的持久属性。 mappedBy 的值应该是持久属性。因此,将其更改为任何应该是反面的内容。也许你希望它是“parentId”或“childId”,我不知道,不能两者都是。

那么您仍然会遇到以下问题:

  • No @Id for TableA
  • public TableB_id :大写 I
  • public class TableB_Id 没有实现 Serialized (它应该是因为它被用作 id)。
  • “public void setTableBSet(Set tableBSet) {” 也许元素类型应该是 TableB,
  • TableA 没有 id
  • 在 TableB 中“return this.parentTable”,您没有这样的变量,
  • 您使用基于属性的访问而不需要设置器

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:

  • No @Id for TableA
  • public TableB_id : capital I
  • public class TableB_Id doesn't implement Serializable (it should because it is used as id).
  • "public void setTableBSet(Set tableBSet) {" Maybe element type should be TableB
  • there is no id for TableA
  • In TableB "return this.parentTable" you do not have such a variable
  • you use property based access without setters
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文