简体   繁体   English

使用实体映射映射作为JPA中的键

[英]Mapping Map with an Entity as a key in JPA

Is it possible to map: 是否可以映射:

@ManyToMany(cascade = Array(CascadeType.ALL), fetch = FetchType.EAGER)
Map<Entity, Double> quantities;

instead of: 代替:

Map<Double, Entity>

?

If not, how do you map for example Item quantities in a shop basket (without dedicated table)? 如果没有,您如何映射商店购物篮中的示例商品数量(没有专用表格)?

Thanks in advance, Etam. 在此先感谢,Etam。

Yes, it can be mapped without additional entity, see, for example, Hibernate: mapping many-to-many to Map . 是的,它可以在没有附加实体的情况下进行映射,例如,参见Hibernate:将多对多映射到Map

Also note that dedicated table will be used anyway, it just can be mapped as an entity, or not. 另请注意,无论如何都会使用专用表,它只是可以映射为实体,或者不是。

You can using an @ElementCollection and a @MapKeyJoinColumn, 你可以使用@ElementCollection和@MapKeyJoinColumn,

see http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Map_Key_Columns_.28JPA_2.0.29 http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Map_Key_Columns_.28JPA_2.0.29

But it is kind of an unusual model, you may be better off create an Entity to map to the join table instead. 但它是一种不寻常的模型,你可能最好创建一个实体来映射到连接表。

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

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