title: JPA Map w/ Entity Key and Entity Value
body: <p>Key Entity:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity public class KeyEntity
  {
      @Id @GeneratedValue(strategy = GenerationType.TABLE)
      public Long id;
  
      public String handle;
  
      public boolean equals(Object o) {
          KeyEntity oke = (KeyEntity) o;
          return handle != null ? handle.equals(oke.handle) : oke.handle == null;
      }
  
      public int hashCode() {
          return handle != null ? handle.hashCode() : 0;
      }
  }
  </code></pre>
  
  <p>Value Entity:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity public class ValueEntity
  {
      @Id @GeneratedValue(strategy = GenerationType.TABLE)
      public Long id;
  
      @ManyToOne
      public KeyEntity key;
  
      public String value;
  
      public boolean equals(Object o) {
          ValueEntity ove = (ValueEntity) o;
          return key != null ? key.equals(ove.key) : ove.key == null;
      }
  
      public int hashCode() {
          return key != null ? key.hashCode() : 0;
      }
  }
  </code></pre>
  
  <p>Container Entity:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity public class ContainerEntity
  {
      @Id @GeneratedValue(strategy = GenerationType.TABLE)
      public Long id;
  
      @OneToMany @MapKey(name = "key")
      public Map&lt;KeyEntity, ValueEntity&gt; values = new HashMap&lt;KeyEntity, ValueEntity&gt;();
  }
  </code></pre>
  
  <p>Main:</p>
  
  <pre class="lang-java prettyprint-override"><code>KeyEntity k1 = new KeyEntity();
  k1.handle = "k1";
  em.persist(k1);
  
  KeyEntity k2 = new KeyEntity();
  k2.handle = "k2";
  em.persist(k2);
  
  ValueEntity v1 = new ValueEntity();
  v1.key = k1;
  v1.value = "v1";
  em.persist(v1);
  
  ValueEntity v2 = new ValueEntity();
  v2.key = k2;
  v2.value = "v2";
  em.persist(v2);
  
  ContainerEntity ce = new ContainerEntity();
  ce.values.put(k1, v1);
  ce.values.put(k2, v2);
  em.persist(ce);
  
  // display number of values
  System.out.println(ce.values.size());
  
  // create new transaction
  em.getTransaction().commit();
  em.close();
  em = emf.createEntityManager();
  em.getTransaction().begin();
  
  // find our container and inspect the number of values
  ce = em.find(ContainerEntity.class, ce.id);
  System.out.println(ce.values.size());
  </code></pre>
  
  <p>If I add several key-value pairs to a <code>ContainerEntity</code> and then reload said container, only one key-value pair will be present. If you look at the output of running the above main function, first '2' is printed, followed by '1'.</p>
  
  <p>I can see that this is because of <code>KeyEntity.hashCode</code> - when inserting into the <code>HashMap</code> <code>KeyEntity.handle</code> is <code>null</code>, so all pairs will have the same hash code. <code>KeyEntity.id</code> is populated at this point - if I base the hash code off of this field everything works out. Also if I change the key to be a <code>String</code> it is loaded in time for the calls to <code>hashCode</code>.</p>
  
  <p>How can I change my mappings in <code>ContainerEntity</code> so that <code>KeyEntity.handle</code> is loaded when it is placed inside the map, so <code>hashCode</code> can use it?</p>