开发者

Hibernate: Parent-Child Relationship to Itself

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!


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:


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜