From the JPA 2.0 specification:
Relationship mappings defined within an embedded id class are not supported.
See also JPA - EmbeddedId with @ManytoOne.
EDIT:
Hibernate 4.3.1.Final implements JPA 2.1, so my first guess was wrong. Mea culpa.
The problem with the mapping is the use of @ElementCollection
with an embeddable CompositeKey
referencing to the entity ReferencedEntity
. The exception in AbstractPropertyMapping
is thrown when Hibernate looks for a reference from CompositeValue
(sic!) to ReferencedEntity
. However as no such relation exists, the exception is thrown. For me it is unclear if this is a Hibernate bug or according the JPA 2.1 specs.
I see following solutions.
Solution 1
Change CompositeKey
to an Entity
adding an id
field. In this case the @OneToMany
annotation could be used:
@Entity
public class CompositeKey {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ManyToOne(targetEntity = ReferencedEntity.class)
private ReferencedEntity key1;
@Basic
private Integer key2;
}
Solution 2
Add an additional entity CompositeSampler
containing CompositeKey
as the ID and CompositeValue
as an additional attribute. MyEntity
will contain a list of CompositeSampler
.
Solution 3
Add a reference from CompositeValue
to ReferencedEntity
.
By the way, according the JPA 2.1 specs: "If an embeddable class is used as a map key, the embeddable class must implement the hashCode and equals methods consistently with the database columns to which the embeddable is mapped".