【问题标题】:How to map Map<String, EMBEDDABLE> using Eclipselink/MongoDB @NoSQL如何使用 Eclipselink/MongoDB @NoSQL 映射 Map<String, EMBEDDABLE>
【发布时间】:2012-09-01 10:36:41
【问题描述】:

我尝试了各种方法,例如最简单的:

// 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<>();

【问题讨论】:

    标签: java mongodb jpa eclipselink nosql


    【解决方案1】:

    您应该能够使用 @MapKey 从 Attribute 对象定义映射键(我假设它的名称)。

    EclipseLink 当前的 NoSQL 支持不支持@MapKeyColumn、@MapKeyJoinColumn 和 @MapKeyClass。

    请记录一个关于异常的错误,你应该得到一个更好的错误,或者映射键应该被更好地默认。

    【讨论】:

    • 你是绝对正确的。我刚刚测试了您的建议,它现在按预期工作。谢谢!
    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    相关资源
    最近更新 更多