How to map a <String, EMBEDDABLE> map using Eclipselink / MongoDB @NoSQL
I tried different things, for example. simplest:
// Value object @Embeddable @NoSql(dataFormat=DataFormatType.MAPPED) public class Attribute implements Serializable { @Basic private String someProp; // ... getter/setter omitted } @Entity @NoSql(dataFormat=DataFormatType.MAPPED) public class FancyEntity implements Serializable { @Id @GeneratedValue @Field(name="_id") private String id; @ElementCollection private Map<String,Attribute> attributes = new HashMap<>(); // ... getter/setter omitted } but this causes an error:
Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.1.v20120825fb0a20b): org.eclipse.persistence.exceptions.EntityManagerSetupException Exception Description: Predeployment of PersistenceUnit [myPersistenceUnit] failed. Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.eis.mappings.EISCompositeCollectionMapping cannot be cast to org.eclipse.persistence.mappings.foundation.MapComponentMapping at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:221) at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1541) at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1532) at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:208) Any idea?
I am using the latest version 2.4.1-SNAPSHOT on Glassfish 3.1.2.
Decision
Add the @MapKey annotation and select the desired key field inside your implemented class:
@ElementCollection @MapKey(name="name") private Map<String,Attribute> attributes = new HashMap<>(); +4
1 answer
You should be able to use @MapKey to determine the map key from the Attribute object (I assume its name).
@MapKeyColumn, @MapKeyJoinColumn, and @MapKeyClass are not supported by current NoSQL support from EclipseLink.
Please register an exception error, however, you should get a more efficient error or better use the card key.
+2