简体   繁体   English

JPA映射MAP <Entity, Embedable>

[英]JPA mapping MAP<Entity, Embedable>

I've 2 classes: 我有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; }
}

The structure of db is db的结构是
es_item has columns: inventory_item_id is key, some orther columns. es_item具有以下列:inventory_item_id是键,另一些列。
es_bom_level has columns: inventory_item_id, component_item_id. es_bom_level的列为:inventory_item_id,component_item_id。 with component_item_id is fk to es_item. 与component_item_id对应的是es_item。
the my mapping does not work. 我的映射不起作用。 I see the error 我看到错误
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)
Please help me. 请帮我。

Sorry for the delay in response as I was stuck with other issues :-( 很抱歉由于我遇到其他问题而延迟了回复:-(

Please try the below code:- 请尝试以下代码:

@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
}

A catch here is that this setup creates an additional column and constraint in you es_bom_levels table which is as expected (my guess) after I went through the below blog 这里要注意的是,此设置在es_bom_levels表中创建了一个附加列和约束,这在我浏览以下博客后符合预期(我的猜测)

mappingembeddable 可嵌入的映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM