title: JPA cascade persistence with entity ElementCollection keys
body: <p>I have two JPA entities like this:</p>
  
  <pre><code>@Entity
  class Foo {
      @Id
      private long id;
      // ...
  }
  
  @Entity
  class Bar {
      @ElementCollection(targetClass = String.class, fetch = FetchType.LAZY)
      @MapKeyJoinColumn(name = "foo_id", referencedColumnName = "id")
      @MapKeyClass(Foo.class)
      @Column(name = "content")
      @CollectionTable(name = "bar_foo_content",
                       joinColumns = @JoinColumn(name = "bar_id", referencedColumnName = "id"))
      @ManyToMany(cascade = CascadeType.ALL)
      private Map&lt;Foo, String&gt; fooContent = Maps.newHashMap();
      // ...
  }
  </code></pre>
  
  <p>As you can see, the <code>fooContent</code> field forms a many-to-many relation between <code>Bar</code> and <code>Foo</code>, so I thought it would be appropriate to use <code>@ManyToMany</code> to specify cascading for the field. However, when trying to persist a <code>Bar</code> with a couple of <code>Foo → String</code> values in the map, I get the following exception:</p>
  
  <pre><code>javax.persistence.RollbackException: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: &lt;&lt;instance of Foo&gt;&gt;
  </code></pre>
  
  <p>Clearly, EclipseLink does not cascade the persistence of my <code>Foo</code> instances. How should I annotate <code>fooContent</code> to get cascaded persists working?</p>