【发布时间】:2011-09-30 07:16:43
【问题描述】:
我有两张桌子:
表名: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;
}
提前谢谢你!
【问题讨论】:
-
似乎您正在实现复合映射。看看我的帖子是否有帮助,因为它已经在工作了stackoverflow.com/questions/5100191/…
标签: hibernate jpa annotations relationship