2012-09-01 62 views
4

我嘗試過各種各樣的事情,例如,最簡單的:如何使用Eclipselink/MongoDB映射Map <String,EMBEDDABLE> @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<>(); 

回答

2

您應該能夠使用@MapKey定義從地圖鍵屬性對象(我假設它的名字)。

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

請記錄有關異常的錯誤,但應該得到一個更好的錯誤,或者應該默認地圖鍵更好。

+0

你是絕對正確的。我只是測試了你的建議,現在按預期工作。謝謝! – matthew

+0

提交了一個bug,請參閱https://bugs.eclipse.org/bugs/show_bug.cgi?id=388789 – matthew

相關問題