Using @ElementCollection to persist a Collection of Enum

4,236 views
Skip to first unread message

roberto_sc

unread,
Aug 29, 2012, 12:46:03 PM8/29/12
to google-a...@googlegroups.com

I'm moving from JPA 1 to JPA 2 in my Gooogle App Engine project, and for that I changed the annotations for collections of enums, like this case:

//  @OneToMany(cascade = CascadeType.ALL) // JPA 1
   
@ElementCollection(targetClass=PositioningType.class)
   
private List<PositioningType> bestPositionings;


PositioningType is an enum that is not annotated.

That code is giving me the following exception:

javax.persistence.PersistenceException: The MetaData for the element class "com.olympya.datamodel.model.PositioningType" of the collection field "com.olympya.datamodel.model.Player.bestPositionings" was not found.
at org
.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:302)
at org
.datanucleus.api.jpa.JPAEntityTransaction.commit(JPAEntityTransaction.java:122)
at com
.olympya.Services.mergeUser(Services.java:102)
...
Caused by: org.datanucleus.exceptions.NucleusUserException: The MetaData for the element class "com.olympya.datamodel.model.PositioningType" of the collection field "com.olympya.datamodel.model.Player.bestPositionings" was not found.
at com
.google.appengine.datanucleus.scostore.AbstractFKStore.<init>(AbstractFKStore.java:120)
at com
.google.appengine.datanucleus.scostore.FKListStore.<init>(FKListStore.java:83)
at com
.google.appengine.datanucleus.DatastoreManager.newFKListStore(DatastoreManager.java:472)
at org
.datanucleus.store.mapped.MappedStoreManager.getBackingStoreForCollection(MappedStoreManager.java:798)
at org
.datanucleus.store.mapped.MappedStoreManager.getBackingStoreForField(MappedStoreManager.java:709)
at org
.datanucleus.store.types.sco.backed.ArrayList.<init>(ArrayList.java:99)
at sun
.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...

roberto_sc

unread,
Aug 30, 2012, 12:19:10 PM8/30/12
to google-a...@googlegroups.com
No opinions at all? Does it look like that I'm doing the right thing?
I just found out that it also doesn't work for a list of integers, possibly any collection of any simple type:

@ElementCollection
@Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
//private List<PositioningType> bestPositionings = new ArrayList<PositioningType>();
private List<Integer> bestPositionings;


Results in:

Caused by: org.datanucleus.exceptions.NucleusUserException: The MetaData for the element class "java.lang.Integer" of the collection field "com.olympya.datamodel.model.Player.bestPositionings" was not found.

at com
.google.appengine.datanucleus.scostore.AbstractFKStore.<init>(AbstractFKStore.java:120)
at com
.google.appengine.datanucleus.scostore.FKListStore.<init>(FKListStore.java:83)
at com
.google.appengine.datanucleus.DatastoreManager.newFKListStore(DatastoreManager.java:472)
at org
.datanucleus.store.mapped.MappedStoreManager.getBackingStoreForCollection(MappedStoreManager.java:798)
at org
.datanucleus.store.mapped.MappedStoreManager.getBackingStoreForField(MappedStoreManager.java:709)
at org
.datanucleus.store.types.sco.backed.ArrayList.<init>(ArrayList.java:99)

:(

Eduardo Perrino

unread,
Aug 31, 2012, 2:42:33 AM8/31/12
to google-a...@googlegroups.com
Hi Roberto,

@ElementCollection annotation have to be used to contain embedded entities, therefore you're have to be sure that PositioningType class is annotated with @Embeddable. 

 The next example works fine for me:

//Class Product
@ElementCollection(fetch = FetchType.EAGER)
 public Set<Rate> rates;

//Class Rate
@Embeddable
public class Rate {
   public String name;
}


Regards.


2012/8/30 roberto_sc <roberto...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/M9bihLLrsTAJ.

To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Roberto Caldas

unread,
Aug 31, 2012, 9:53:32 AM8/31/12
to google-a...@googlegroups.com
Hello Eduardo

No, the documentation states that it can be used for simple types also: "Defines a collection of instances of a basic type or embeddable class" (http://docs.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html).

This 2 links points that ElementCollection should be used with enums:
http://docs.oracle.com/javaee/6/api/javax/persistence/Enumerated.html
http://stackoverflow.com/questions/416208/jpa-map-collection-of-enums
Reply all
Reply to author
Forward
0 new messages