我有2个班:
@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是键,有些是其他列。
es_bom_level包含columns:inventory_item_id,component_item_id。 with 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)
请帮帮我。
答案 0 :(得分:0)
很抱歉由于我遇到其他问题而导致回复延迟: - (
请尝试以下代码: -
@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表中创建一个额外的列和约束,这是我在浏览下面的博客后的预期(我的猜测)