我有两张桌子:
表名: TABLE_A
A_ID
A_CODE
A_DESC
表名: TABLE_B
B_ID
B_TABLE_A_PARENT_ID
B_TABLE_A_CHILD_ID
其中: 可以在TABLE_B的B_TABLE_A_PARENT_ID和B_TABLE_A_CHILD_ID中输入TABLE_A的A_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.TableA.tableB中的com.parentchild.TableB.tableA
我认为违规代码是TableA上面的第一个代码块,但我不知道该怎么做。请帮助我。
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tableA", cascade = CascadeType.ALL)
public Set<TableB> getTableBSet() {
return tableBSet;
}
提前谢谢!
答案 0 :(得分:0)
最近,我使用复合主键映射@Many-To-Many
:您可以轻松将其更改为@One-To-Many
。
这里有完整的代码和解释:
Mapping ManyToMany with composite Primary key and Annotation:
答案 1 :(得分:0)
该错误消息的原因是没有名为“tableA”的持久属性。 mappedBy的值应该是持久属性。所以把它改成任何应该是反面的东西。也许你想要它是“parentId”或“childId”,我不知道,不能两者兼而有之。
然后你仍会遇到以下问题: