Persisting an entity with an @ElementCollection that is a map with a composite key fails. A QueryException gets thrown during initialization of hibernate.
Simple test setup follows:
import javax.persistence.*;
import java.util.Map;
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ElementCollection
private Map<CompositeKey, CompositeValue> map;
@Embeddable
public static class CompositeKey {
@ManyToOne
private ReferencedEntity key1;
@Basic
private Integer key2;
}
@Embeddable
public static class CompositeValue {
@Basic
private Integer val1;
@Basic
private Integer val2;
}
}
import javax.persistence.*;
@Entity
public class ReferencedEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
}
It's strange that hibernate generates the schema correctly, if I rename MyEntity.CompositeKey.key1 to val1.
If the map referenced an Integer instead of CompositeValue, the following Exception raises:
I was able to prevent this exception by renaming key1 to id.
|