Note that there are some explanatory texts on larger screens.

plurals
  1. PO@ElementCollection with Map<Entity, Embeddable> where Entity is a field of the Embeddable
    primarykey
    data
    text
    <p>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'm doing something stupid,</p> <p>My domain model has a "Portfolio", which contains zero or more "open positions". A position consists of an "Instrument" (which is a JPA Entity) and a price (double). The portfolio is as follows:</p> <pre><code>@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&lt;Instrument, OpenPosition&gt; positions = new HashMap&lt;Instrument, OpenPosition&gt;(); .... </code></pre> <p>The OpenPosition Embeddable is as follows:</p> <pre><code>@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; .... </code></pre> <p>and the Instrument entity is:</p> <pre><code>@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(); .... </code></pre> <p>When I try to use this, the schema is created and I am able to persist portfolios, but when trying to retrieve them, I get a NullPointerException in the hashCode method of the Instrument class. It seems JPA is trying to get the hash code to build the Map key, but the Instrument object has not been loaded.</p> <p>I can see through debugging that although the id is set in the Instrument object, all the other fields are null.</p> <p>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? If so, what am I screwing up. And if not, is the best workaround to use the id of the Instrument entity as the key instead?</p> <p>Thanks in advance.</p> <p>p.s. I'm using the hibernate 4.1.4 JPA implementation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload