@ElementCollection with a map <Entity, Embeddable>, where Entity is an embedded field

After searching for JPA documents and various posts, I am confused if the following is possible with JPA2.0. I'm just starting out with JPA, so excuse me if I'm doing something stupid,

My domain model has a "Portfolio" that contains zero or more "open positions". A position consists of a β€œTool” (which is a JPA entity) and a price (double). The portfolio is as follows:

@Entity (name = "portfolio") public class Portfolio { @Id @Column (name = "id") @GeneratedValue private long id; @ElementCollection (fetch = FetchType.EAGER) @CollectionTable (name = "portfolio_entry", joinColumns = @JoinColumn (name = "portfolio_id")) private final Map<Instrument, OpenPosition> positions = new HashMap<Instrument, OpenPosition>(); .... 

The nested version of OpenPosition is as follows:

 @Embeddable public class OpenPosition extends Position { @ManyToOne (targetEntity = InstrumentImpl.class, optional = false) @JoinColumn (name = "instrument_id", nullable = false) protected Instrument instrument; @Column (name = "price", nullable = false) protected double price; .... 

and the Instrument object:

 @Entity (name="instrument") public class Instrument { @Id @Column(name = "id") @GeneratedValue private long id; @Column(name = "isin", nullable = false) private String isin; .... @Override public int hashCode() { int hash = 17; hash = 31 * hash + isin.hashCode(); .... 

When I try to use this, a diagram is created and I can save the portfolio, but when I try to get them, I get a NullPointerException in the hashCode method of the Instrument class. JPA seems to be trying to get a hash code to create a map key, but the Instrument has not been loaded.

I see through debugging that although the identifier is set in the Instrument object, all other fields have a null value.

So my question is: does JPA2.0 allow using ElementCollection, where the key is Entity, which is also present as an Embeddable value field? If so, what am I screwing up. And if not, is it best to use the Instrument object identifier instead of the key?

Thanks in advance.

ps I am using hibernate 4.1.4 JPA implementation.

+4
source share
1 answer

So my question is: does JPA2.0 allow using ElementCollection, where the key is Entity, which is also present as an Embeddable value field?

Yes, I managed to do this using 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 @Entity and FreightBid is @Embedded

I was able to save and get an Entity that contains this map correctly.

what am I screwing up.

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

It would also be better to avoid using fields other than id in the hashCode method of the object, which acts as the map key ... you, the JPA developer, could damage things.

+2
source

All Articles