I believe the issue to this problem is in PK-FK(Primary Key - Foreign Key) constraints.
In summary:
Parent:
| keyIdOne | keyIdTwo |
|----------|----------|
| 1 | a |
| 1 | b |
| 3 | a |
Child:
| id | keyIdOne |
|----|----------|
| 2 | 1 |
| 3 | 3 |
A foreign key must reference a single row in the table that it's referencing (http://en.wikipedia.org/wiki/Foreign_key). Which row in Parent does does Child.keyIdOne
reference if Child has keyIdOne = 1
?
I thought there was a possibility of this working by using the @MapsId() annotation. However this will only work in inverse. Example:
Parent:
| keyIdOne |
|----------|
| 1 |
| 3 |
Child:
| keyIdOne | keyIdTwo |
|----------|----------|
| 1 | a |
| 1 | b |
| 3 | a |
In this case you can have the Child map part of its composite key to the Parent with:
@MapsId("keyIdOne")
private Parent parent;
@MapsId only works in a OnetoOne and ManyToOne (http://docs.oracle.com/javaee/6/api/javax/persistence/MapsId.html)