I have two tables with these primary keys:
TABLE A TABLE B
---------- ----------
| colA |-----> | colX |
| colB |-----> | colY |
| colC |-----> | colW |
|__________| | colZ |
|__________|
Basically I need to define this relationship in JPA 1.0.
I tried to map entity of tableA with this code:
@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
All I get is this error :
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
Any help ?
EDIT*
Either Table A and TableB got @EmbeddedId primary key class with their own pk cols stated on top.
The code below explains better the situation
// 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
}
Your mapping is partly incorrect. Try the following:
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;
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.