【发布时间】:2016-09-12 10:57:44
【问题描述】:
我有两个带有这些主键的表:
TABLE A TABLE B
---------- ----------
| colA |-----> | colX |
| colB |-----> | colY |
| colC |-----> | colW |
|__________| | colZ |
|__________|
基本上我需要在 JPA 1.0 中定义这种关系。
我尝试使用以下代码映射 tableA 的实体:
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, targetEntity=TableB.class)
@JoinColumns({
@JoinColumn(name="colX", referencedColumnName="colA", insertable=false, updatable=false),
@JoinColumn(name="colY", referencedColumnName="colB", insertable=false, updatable=false),
@JoinColumn(name="colW", referencedColumnName="colC", insertable=false, updatable=false)
})
private Set<TableB> tableB;
..get and set
我得到的只是这个错误:
org.hibernate.AnnotationException: Unable to map collection TableB
Caused by: org.hibernate.AnnotationException: referencedColumnNames(colA, colB, colC) of tableB referencing tableA not mapped to a single property
有什么帮助吗?
编辑*
表 A 和表 B 都获得了 @EmbeddedId 主键类,并在顶部声明了它们自己的 pk cols。
下面的代码更好地解释了这种情况
// TABLE A PKey Entity
@Embeddable
class TableAPKey
{
@Column
String colA; // get and set
@Column
String colB; // get and set
@Column
String colC; // get and set
}
// TABLE A Entity
class TableA
{
@EmbeddedId
TableAPKey key; // get and set
}
// TABLE B PKey entity
@Embeddable
class TableBPKey
{
@Column
String colX; // get and set
@Column
String colY; // get and set
@Column
String colW; // get and set
@Column
String colZ; // get and set NOT USED IN RELATIONSHIP with TableA
}
// TABLE B Entity
class TableB
{
@EmbeddedId
TableBPKey key; // get and set
}
【问题讨论】:
-
colA、colB、colC是TableA的复合主键吗?请发布TableA实体的ID字段。 -
我通过插入实体 A 和 B 的定义改进了问题
-
B和A有关系吗?
标签: java hibernate jpa one-to-many hibernate-onetomany