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!