如何使用Eclipselink / MongoDB @NoSQL </string,>映射Map <string,embeddable =“”>

时间:2012-09-01 10:36:41

标签: java mongodb jpa eclipselink nosql

我尝试过各种各样的事情,例如最简单:

// 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
}

但这会产生错误:

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)

有什么想法吗?

我在Glassfish 3.1.2上使用最新的2.4.1-SNAPSHOT。

解决方案

添加@MapKey批注,并在嵌入类中选择所需的关键字段:

   @ElementCollection
   @MapKey(name="name")
   private Map<String,Attribute> attributes = new HashMap<>();

1 个答案:

答案 0 :(得分:2)

您应该能够使用@MapKey从Attribute对象中定义地图密钥(我​​假设其名称)。

EclipseLink目前的NoSQL支持不支持@MapKeyColumn,@ MapKeyJoinColumn和@MapKeyClass。

请记录有关异常的错误,但是您应该得到更好的错误,或者应该更好地默认地图键。