0

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;
Cris
  • 1
  • 1
  • 5
  • Hi Cris! This problem is resolved here: http://stackoverflow.com/questions/23837561/jpa-2-0-many-to-many-with-extra-column/29116687#29116687 – Erik Lucio Dec 21 '15 at 18:45
  • ok, i saw it.but my doses column map manytomany the ingredients so it's q bit different i think, it's a manytomany from the intermediate table to the ingredients table... Or not? – Cris Dec 21 '15 at 20:06

2 Answers2

1

I think Its the same.

I will explain it:

You need a ManyToMany table between recipes and ingretients tables with extra column: dose. Then:

First of all, create a RecipesIngredientsPK class like this (because Its a multiple PK):

@Embeddable
public class RecipesIngredientsPK implements Serializable {

    @Column(name = "RECIPE_ID")
    private Long recipe_id;

     @Column(name = "INGREDIENT_ID")
    private Long ingredient_id;
}

Now, create a RecipesIngredients class to represent the ManyToMany table between recipes and ingretients tables:

@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;
}

And in the Recipes class you can access to dose like this:

    @OneToMany(mappedBy = "recipe")
    private Set<RecipesIngredients> recipesIngredients = new HashSet<RecipesIngredients>();

This Set includes the ingredients and their doses. Your Ingredient and doses fields are included in private Set<RecipesIngredients> recipesIngredients.

Community
  • 1
  • 1
Erik Lucio
  • 948
  • 7
  • 8
0

This requirement is a manytomany relationship with additional columns in the mapping table. In your case it is amount of each ingredient for that recipe.

You can refer to the following tutorial by Mkyong.

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

prem kumar
  • 5,641
  • 3
  • 24
  • 36