Hibernate: mapping many-to-many to Map

前端 未结 1 780
攒了一身酷
攒了一身酷 2021-02-06 09:47

I am developing an application which deals with two following entities: Products (let\'s name it as X, Y, Z) and Materials (a, b, c, ...). It\'

1条回答
  •  一向
    一向 (楼主)
    2021-02-06 10:39

    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 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".

    0 讨论(0)
提交回复
热议问题