@ElementCollection with Map where Entity is a field of the Embeddable

后端 未结 1 1358
生来不讨喜
生来不讨喜 2021-01-01 03:59

After searching through the JPA docs and various posts, I\'m confused as to whether the following is possible with JPA2.0. I\'m just starting out with JPA so excuse me if I\

相关标签:
1条回答
  • 2021-01-01 04:26

    So my question is, does JPA2.0 allow an ElementCollection where the key is an Entity that is also present as a field of the Embeddable value?

    Yes, i managed to do it with this mapping:

    @ElementCollection( targetClass = FreightBid.class )
    @MapKeyJoinColumn( name = "carrier_id", referencedColumnName = "id" )
    @CollectionTable( name = "freight_bid",
        joinColumns = @JoinColumn( name = "offer_pool_id" ) )
    @Access( AccessType.FIELD )
    private Map<Carrier,FreightBid> bidsByCarrier;
    

    In my case, Carrier is an @Entity and FreightBid is an @Embedded

    I've been able to persist and retrieve the Entity that contains this map correctly.

    what am I screwing up.

    You should remove the field protected Instrument instrument; from OpenPosition class and instead use the annotation @MapKeyJoinColumn on the map field in Portfolio class to declare wich column should be used as join column to the map key.

    Also it would be best to avoid using other fields than id in the hashCode method of an object wich act as a map key... you JPA implementor might screw things up.

    0 讨论(0)
提交回复
热议问题