There is possibly something fundamental I don't understand about the semantics of JPA @MapKey element. I am trying to save a Map that has entity keys and entity values. The Schema is auto generated by hibernate. It generates a join table that maps the values 开发者_StackOverflow社区entities to the containing entity (that has the Map property) and ignores the keys. so effectively it just treats it as a collection of values and ignores the keys, as far as I can tell. what am i missing here ? Thank you
@Entity
public class PracticeMap {
@javax.persistence.OneToMany(cascade = CascadeType.ALL)
@javax.persistence.MapKey
public Map<KeySample, ValueSample> getMap1() {
return map1;
}
//more unrelated/standard bits here
}
Look at the javadoc of @MapKey
- it's used when you need to treat particular fields of the value entity as keys.
If your key and value should be different entities, you need to use @MapKeyJoinColumn
(introduced in JPA 2.0).
精彩评论