Hibernate: mapping many-to-many to Map

一笑奈何 提交于 2019-12-03 08:07:05

Since nobody posted the solution without annotations, I'll show the solution with JPA 2.0 @ElementCollection annotation:

@ElementCollection
@CollectionTable(name = "Recipes", 
    joinColumns = @JoinColumn(name = "product_id"))
@MapKeyJoinColumn(name = "material_id")
@Column(name = "count")
private Map<Material, Integer> recipe;

Also note that since class of values of your map is Integer, solution without annotations is likely to be documented as "collection mapping" rather than "entity relationship mapping".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!