简体   繁体   English

JPA映射到Map <Object, Interger>

[英]JPA mapping to Map<Object, Interger>

I've 3 tables: - lots: has column gen_id 我有3张桌子:-很多:列gen_id
- onhand_quantities: has column gen_id,sub_inventory_code, quantity -onhand_quantities:具有gen_id,sub_inventory_code列,数量
- subinventory: has column sub_inventory_code -子库存:具有列sub_inventory_code

I map to 2 objects: Lots, SubInventory and I would like to mapping in object Lots with property is: Map<SubInventory, Integer> getOnhandQuantity() 我映射到2个对象:Lots,SubInventory,我想映射到对象Lots中的属性为: Map<SubInventory, Integer> getOnhandQuantity()
Integer type in this Map is value of quantity column. 此映射中的整数类型是数量列的值。 Please help me. 请帮我。

Just a brief search in Hibernate document give me information on using an associated entity as key of a Map (aka Ternary Association ) 只需在Hibernate文档中进行简短搜索即可了解有关将关联实体用作Map(也称为Ternary Association )键的信息

Quoted from the manual: 从手册中引用:

There are three possible approaches to mapping a ternary association. 映射三元关联有三种可能的方法。 One approach is to use a Map with an association as its index: 一种方法是使用带有关联的Map作为其索引:

Example 7.31. 示例7.31 Ternary association mapping 三元关联映射

 @Entity public class Company { @Id int id; ... @OneToMany // unidirectional @MapKeyJoinColumn(name="employee_id") Map<Employee, Contract> contracts; } // or <map name="contracts"> <key column="employer_id" not-null="true"/> <map-key-many-to-many column="employee_id" class="Employee"/> <one-to-many class="Contract"/> </map> 

A second approach is to remodel the association as an entity class. 第二种方法是将关联重塑为实体类。 This is the most common approach. 这是最常见的方法。 A final alternative is to use composite elements, which will be discussed later. 最后一种选择是使用复合元素,这将在后面讨论。

To map it back to your original example: Company -> Lots, Employee -> SubInventory, Contract -> OnhandQuantity 要将其映射回您的原始示例:公司->批次,员工->子库存,合同-> OnHandQuantity

Personally I would rather make them as a simple relationship, and construct Map etc on the fly whenever it is needed. 我个人更希望将它们作为一个简单的关系,并在需要时随时动态构建Map等。


Haven't tried, but a further look get me to an example in @MapKeyColumn , which in combination with the above sample, should give something reasonable like: 还没有尝试过,但是进一步了解一下@MapKeyColumn中的示例,结合上面的示例,应该可以提供类似以下内容的示例:

class Lot {
    @ElementCollection
    @MapKeyJoinColumn(name="sub_inventory_id")
    @CollectionTable(name="onhand_quantities")
    @Column(name="quantity")
    private Map<SubInventory, Integer> onhandQuantities;

}

The below code in the Lots entity should do. Lots实体中的以下代码应执行此操作。

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "ONHAND_QUANTITIES", joinColumns= @JoinColumn(name="GEN_ID"))
@MapKeyJoinColumn(name="SUBINVENTORY_CODE")
@Column(name="QUANTITY")
private Map<SubInventory, Integer> onHandQuantity;

I am trying to test this. 我正在尝试测试。

I got this solution from the below link. 我从以下链接获得了该解决方案。

Reference key issue while doing many to many relationship using @ElementCollection, @MapKeyJoinColumn 使用@ ElementCollection,@ MapKeyJoinColumn做多对多关系时参考关键问题

@Entity
@Table(name = "LOTS")
public class Lots implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name = "GEN_ID")
    private Long id;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "ONHAND_QUANTITIES", joinColumns= @JoinColumn(name="GEN_ID"))
    @MapKeyJoinColumn(name="SUBINVENTORY_CODE")
    @Column(name="QUANTITY")
    private Map<SubInventory, Integer> onHandQuantity;

   ......// getters and setters
}


@Entity
@Table(name = "SUBINVENTORY")
public class SubInventory implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name="SUBINVENTORY_CODE")
    private Long id;

    ......// getters and setters
}

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

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