我需要为这些表创建带有注释的M到N映射:
Table Food : Columns: id, description, size, type
Table Ingredients: Columns: id, description, price
Table Food_Ingredients: Columns: food_id (FK), ingredient_id (FK), quantity
类:
class Food {
/*mapped fields setter/getters*/
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.DETACH)
@JoinTable( name = "food_ingredient",
joinColumns = { @JoinColumn(name = "food_id", nullable = false) },
inverseJoinColumns = { @JoinColumn(name = "ingredient_id", nullable = false) })
private List<Ingredient> ingredients;
}
class Ingredients {/*mapped fields setter/getters*/}
那么,我该如何映射这些实体?
答案 0 :(得分:2)
您如何表示模型中的数量?您应该使用此属性为FoodIngredients创建一个新实体(以及指向其他表的链接),并使用食物和成分中的@OneToMany
注释对其进行映射。
修改:您可以使用http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/作为参考