title: Persisting @ElementCollection of @Embeddable (Google App Engine, datanucleus)
body: <p>I am trying to persist a JPA entity with a collection of custom @Embeddable objects using the JPA2 <a href="http://docs.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html" rel="nofollow">@ElementCollection</a> annotation. Simple example (both classes are enhanced by datanucleus):</p>
  
  <pre><code>@Entity
  public class TestEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @ElementCollection
      private Set&lt;TestEmbeddable&gt; testEmbeddables;
  
      public Set&lt;TestEmbeddable&gt; testEmbeddables() {
          return this.testEmbeddables;
      }
  }
  
  @Embeddable
  public class TestEmbeddable implements Serializable {
      public String s;
  }
  </code></pre>
  
  <p>The persisted Datastore entity, however, will only contain a collection of null values instead of the actual objects:</p>
  
  <pre><code>TestEntity.testEmbeddables = [null, null, ...]
  </code></pre>
  
  <p>Persisting a collection of basic types such as Strings or embedding a single TestEmbeddable object using @Embedded works perfectly fine. <strong>Would someone be able to clarify whether element collections of embeddables are supported by datanucleus-appengine?</strong></p>
  
  <p>While the datanucleus section on <a href="http://www.datanucleus.org/products/accessplatform_3_1/jpa/orm/one_to_many_list.html" rel="nofollow">JPA element collections</a> only gives an example for a String collection, the corresponding <a href="http://www.datanucleus.org/products/accessplatform_3_1/jdo/orm/embedded.html#Collection" rel="nofollow">JDO section</a> uses a custom embedded-only type. The <a href="http://www.datanucleus.org/products/accessplatform_3_1/datastore_features.html" rel="nofollow">feature list</a> further states that embedded collections in general are compatible with GAE, but does not say whether custom types are supported. I also found one other person claiming that <a href="https://groups.google.com/d/msg/google-appengine/cCUNPEOnqsw/W-ZEp7URTU0J" rel="nofollow">this should work</a>.</p>
  
  <p><strong>-- Edit --</strong> </p>
  
  <p>Following DataNucleus' answer, I ran some more tests:</p>
  
  <pre><code>@ElementCollection
  private List&lt;String&gt; stringsElementCollection;
  </code></pre>
  
  <p>--> Works. The individual Strings are persisted as TestEntity.stringsElementCollection = [str1, str2, ...]</p>
  
  <pre><code>@Embedded
  private List&lt;String&gt; stringsEmbedded;
  </code></pre>
  
  <p>--> Same as @ElementCollection. I wonder if the JPA specification covers the use of <a href="http://docs.oracle.com/javaee/6/api/index.html?javax/persistence/ElementCollection.html" rel="nofollow">@Embedded</a> on a collection, though?</p>
  
  <pre><code>@ElementCollection
  private List&lt;TestEmbeddable&gt; embeddablesElementCollection;
  </code></pre>
  
  <p>--> Doesn't work. Instead of the actual TestEmbeddable objects, the Datastore persists only a collection of null values: TestEntity.embeddablesElementCollection = [null, null, ...]</p>
  
  <pre><code>@Embedded
  private List&lt;TestEmbeddable&gt; embeddablesEmbedded;
  </code></pre>
  
  <p>--> This seems to work. The TestEmbeddable.s field is stored as TestEntity.s.0, .s.1, etc. plus a TestEntity.embeddablesEmbedded.size property.</p>
  
  <p><em>(App Engine SDK 1.7.7.1, datanucleus 3.1.3, datanucleus-appengine 2.1.2)</em></p>