[英]How persist Map<String, Embeddable type> using hibernate in mySQL
[英]How to map Map<String, EMBEDDABLE> using Eclipselink/MongoDB @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<>();
您應該可以使用@MapKey從Attribute對象(我假設其名稱)中定義地圖鍵。
EclipseLink當前的NoSQL支持不支持@ MapKeyColumn,@ MapKeyJoinColumn和@MapKeyClass。
但是,請記錄有關該異常的錯誤,您應該得到更好的錯誤,或者應該更好地將map密鑰默認為默認值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.