开发者

difficulties Mapping maps with hibernate using JPA annotations

开发者 https://www.devze.com 2023-03-21 17:47 出处:网络
There is possibly something fundamental I don\'t understand about the semantics of JPA @MapKey element.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消