我有两个类:
@Entity
@Table(name = "es_item")
public class Item {
@Id
@Column(name= "inventory_item_id")
public int getInventoryItemID(){
return inventoryItemID;
}
@ElementCollection(targetClass = ItemConversionAttributes.class, fetch = FetchType.LAZY)
@MapKeyJoinColumn(name="component_item_id", referencedColumnName = "inventory_item_id")
@CollectionTable(name="es_bom_levels", joinColumns = @JoinColumn(name = "inventory_item_id"))
public Map<Item, ItemConversionAttributes> getConversionAttributes(){ return conversionAttributes; }
}
@Embeddable
public class ItemConversionAttributes {
@ManyToOne(targetEntity=Item.class)
@JoinColumn(name="component_item_id", insertable=false, updatable=false)
@NotFound(action=NotFoundAction.IGNORE)
public Item getComponentItem(){ return componentItem; }
}
db的结构是
es_item有列:inventory_item_id是key,一些其他的列
Es_bom_level包含:inventory_item_id, component_item_id。与component_item_id是fk到es_item。
我的映射不起作用。我看到错误Caused by: javax.persistence.PersistenceException: [PersistenceUnit: vmjboss7] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
... 3 more
Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: com.biz.model.Item.conversionAttributes column: component_item_id
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:341)
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:364)
at org.hibernate.mapping.Collection.validate(Collection.java:321)
at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:89)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1298)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
很抱歉,因为我被其他问题困住了,所以延迟了回复:-
请尝试下面的代码:-
@Entity
@Table(name = "es_item")
public class Item {
@Id
@Column(name= "inventory_item_id")
private int inventoryItemID;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name="es_bom_levels", joinColumns = @JoinColumn(name = "component_item_id"))
@MapKeyJoinColumn()
public Map<Item, ItemConversionAttributes> conversionAttributes;
......
// getters and setters
}
@Embeddable
class ItemConversionAttributes {
@ManyToOne(targetEntity=Item.class)
@JoinColumn(name="inventory_item_id", insertable=false, updatable=false)
@NotFound(action=NotFoundAction.IGNORE)
private Item componentItem;
..........
// getters and setters
}
这里的一个问题是,这个设置在es_bom_levels表中创建了一个额外的列和约束,正如我在浏览了下面的博客
之后所期望的那样(我猜)。mappingembeddable