如何将表与中间表和附加相关列spring hibernate mvc连接起来
Posted
技术标签:
【中文标题】如何将表与中间表和附加相关列spring hibernate mvc连接起来【英文标题】:How to connect tables with intermediate table and additional related column spring hibernate mvc 【发布时间】:2015-12-21 17:52:10 【问题描述】:我有以下问题: 创建食谱和成分之间的关系,但我也有一列包含该食谱的每种成分的数量。 最后,我将有 3 张桌子以及食谱和配料之间的多对多关系。 我无法弄清楚将注释放在哪个位置和位置。 这是我要写的内容:
@Entity
@Table(name="recipes")
public class Recipe
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@OneToOne
@JoinColumn(name="id_ingredient", referencedColumnName = "id_ingredient")
private int Ingredient;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="recipes_ingredients",
joinColumns=@JoinColumn(name="id_recipe"),
inverseJoinColumns=@JoinColumn(name="id_ingredient"), @JoinColumn(name="dose"))
private Map<Ingredient, String> doses;
@Column(name="preparation")
private String preparation;
@Column(name="timepreparation")
private long timepreparation;
@Column(name="difficulty")
private int difficulty;
@Entity
@Table(name="ingredients")
public class Ingredient
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="nameing")
private String nameing;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@JoinTable(name="recipes_ingredients",
joinColumns=@JoinColumn(name="id_ingredient", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="id_recipe", referencedColumnName="id"))
private Recipe recipe;
@Entity
@Table(name="recipes_ingredients")
public class Recipes_ingredients
@Id
@OneToMany(fetch=FetchType.LAZY, mappedBy="id_recipe", cascade= CascadeType.ALL )
@Column(name="id_recipe")
private int id_recipe;
@OneToMany(fetch=FetchType.LAZY, mappedBy="id_ingredient", cascade= CascadeType.ALL )
@Column(name="id_ingredient")
private int id_ingredient;
@ManyToOne(fetch=FetchType.LAZY, mappedBy="ricetta", cascade= CascadeType.ALL )
private Recipe recipe;
【问题讨论】:
嗨克里斯!这个问题在这里解决了:***.com/questions/23837561/… 好的,我看到了。但是我的剂量列映射了许多成分,所以我认为它有点不同,它是从中间表到成分表的许多...或者不是? 【参考方案1】:我认为是same。
我会解释的:
您需要一个介于 recipes
和 ingretients
表之间的 ManyToMany 表,并带有额外的列:dose
。那么:
首先,像这样创建一个RecipesIngredientsPK
类(因为它是一个多重PK):
@Embeddable
public class RecipesIngredientsPK implements Serializable
@Column(name = "RECIPE_ID")
private Long recipe_id;
@Column(name = "INGREDIENT_ID")
private Long ingredient_id;
现在,创建一个 RecipesIngredients
类来表示 recipes
和 ingretients
表之间的 ManyToMany 表:
@Entity
@Table(name = "RecipesIngredients")
public class RecipesIngredients implements Serializable
@EmbeddedId
private RecipesIngredientsPK id;
@ManyToOne
@MapsId("RECIPE_ID") //This is the name of attr in RecipesIngredientsPK class
@JoinColumn(name = "RECIPE_ID")
private Recipe recipe;
@ManyToOne
@MapsId("INGREDIENT_ID")
@JoinColumn(name = "INGREDIENT_ID")
private Ingredient ingredient;
@Column(name="dose")
private String dose;
在Recipes
类中,您可以像这样访问剂量:
@OneToMany(mappedBy = "recipe")
private Set<RecipesIngredients> recipesIngredients = new HashSet<RecipesIngredients>();
此Set
包括成分及其剂量。您的Ingredient
和doses
字段包含在private Set<RecipesIngredients> recipesIngredients
中。
【讨论】:
【参考方案2】:此要求是与映射表中的附加列的多对多关系。在您的情况下,它是该食谱的每种成分的数量。
你可以参考下面Mkyong的教程。
http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
【讨论】:
以上是关于如何将表与中间表和附加相关列spring hibernate mvc连接起来的主要内容,如果未能解决你的问题,请参考以下文章
FULL OUTER JOIN 将表与 PostgreSQL 合并
Mysql Workbench 将表和列转换为在 Linux (AWS -EC2) 中不起作用的小写