2011-09-30 67 views
0

我有两个表:休眠:亲子关系到自身

表名: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; 
} 

预先感谢您!

+0

好像你正在实现一个复合映射基于属性的访问。看看我的文章帮助的,因为已经工作http://stackoverflow.com /问题/ 5100191 /休眠-异常,而节能与 - 级联保存更新 –

回答

0

原因该错误消息是,有一个名为“表A”没有持久的财产。 mappedBy的值应该是持久属性。所以把它改为任何应该是反面的东西。也许你希望它是“parentId”或“childId”,我不知道,不能同时存在。

,那么你仍然会有例如以下问题:

  • 没有@Id用于TableA的
  • 公共TableB_id:资本我
  • 公共类TableB_Id没有实现Serializable(它应该是因为它是用作id)。
  • “公共无效setTableBSet(设置tableBSet){” 也许元素的类型应该是表B
  • 没有ID为TableA的
  • TableB中“回归这一点。parentTable”你没有这样的变量
  • 您使用不制定者