我有两张带有这些主键的表:
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
}
答案 0 :(得分:0)
您的映射部分不正确。请尝试以下方法:
class TableA {
// ...
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, targetEntity=TableB.class, mappedBy = "key.tableA")
private Set<TableB> tableB;
}
@Embeddable
class TableBPKey {
@ManyToOne
@JoinColumns({
@JoinColumn(name = "colX", referencedColumnName = "colA"),
@JoinColumn(name = "colY", referencedColumnName = "colB")
@JoinColumn(name = "colW", referencedColumnName = "colC")
})
private TableA tableA;
@Column
String colZ;
// ...
}