title: How do I use JPA to persist a Map (java.util.Map) object inside an entity and ensure the persistence cascades?
body: <p>I have recently started playing around with the Play! Framework for Java, version 1.2.3 (the latest). While testing out the framework, I came across a strange problem when trying to persist a <code>Map</code> object inside a Hibernate entity called <code>FooSystem</code>. The Map object maps a long to a Hibernate entity I have called <code>Foo</code>, with the declaration <code>Map&lt;Long, Foo&gt; fooMap;</code></p>
  
  <p>My problem is as follows: The correct tables are created as I have annotated them. However, when the <code>FooSystem</code> object <code>fs</code> is persisted, the data in <code>fs.fooMap</code> is not!</p>
  
  <p>Here is the code I am using for the entities. First is <code>Foo</code>:</p>
  
  <pre><code>package models.test;
  
  import javax.persistence.Entity;
  import javax.persistence.ManyToOne;
  import play.db.jpa.Model;
  
  @Entity
  public class Foo extends Model
  {
      @ManyToOne
      private FooSystem foosystem;
  
      public Foo(FooSystem foosystem)
      {
          this.foosystem = foosystem;
      }
  }
  </code></pre>
  
  <p>And here is <code>FooSystem</code>:</p>
  
  <pre><code>package models.test;
  
  import java.util.HashMap;
  import java.util.Map;
  import javax.persistence.CascadeType;
  import javax.persistence.Entity;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.ManyToMany;
  import play.db.jpa.Model;
  
  @Entity
  public class FooSystem extends Model
  {
      @ManyToMany(cascade = {CascadeType.ALL, CascadeType.PERSIST})
      @JoinTable(
          name = "fooMap",
          joinColumns = @JoinColumn(name = "foosystem"),
          inverseJoinColumns = @JoinColumn(name = "foo")
      )
      private Map&lt;Long, Foo&gt; fooMap = new HashMap&lt;Long, Foo&gt;();
  
      public FooSystem()
      {
          Foo f1 = new Foo(this);
          Foo f2 = new Foo(this);
          fooMap.put(f1.getId(), f1);
          fooMap.put(f2.getId(), f2);
      }
  
      public Map&lt;Long, Foo&gt; getFooMap()
      {
          return fooMap;
      }
  }
  </code></pre>
  
  <p>Here is the <code>Controller</code> class I am using to test my set-up:</p>
  
  <pre><code>package controllers;
  
  import javax.persistence.EntityManager;
  import models.test.FooSystem;
  import play.db.jpa.JPA;
  import play.mvc.Controller;
  
  public class TestController extends Controller
  {
      public static void index() {
          EntityManager em = JPA.em();
          FooSystem fs = new FooSystem();
          em.persist(fs);
          render();
      }
  }
  </code></pre>
  
  <p>The Play! framework automatically created a transaction for the HTTP request. Although data is inserted into the <code>foo</code> and <code>foosystem</code> tables, nothing is ever inserted into the <code>foomap</code> table, which is the desired result. What can I do about this? What am I missing?</p>