我有兩個表:休眠:親子關係到自身
表名:TABLE_A
A_ID
A_CODE
A_DESC
表名:表-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以創建關聯ip自己。
我的代碼:
@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.hi bernate.AnnotationException:mappedBy引用未知目標實體屬性:com.parentchild.TableA.tableB中的com.parentchild.TableB.tableA
我認爲有問題的代碼是TableA中上面的第一個代碼塊,但我沒有知道做什麼。請幫助我。
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tableA", cascade = CascadeType.ALL)
public Set<TableB> getTableBSet() {
return tableBSet;
}
預先感謝您!
好像你正在實現一個複合映射基於屬性的訪問。看看我的文章幫助的,因爲已經工作http://stackoverflow.com /問題/ 5100191 /休眠-異常,而節能與 - 級聯保存更新 –