I have the following problem: create a relation between recipes and ingredients, but also I have a column containing the amounts of each ingredient for that recipe. In the end I will have 3 tables and a relation manytomany between recipes and ingredients. I can't figure out which and where to put the annotations. Here what I'm trying to write:
@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;