My question is about the code below:
Ingredient.java
@Entity
@Table(name = "recipes_ingredients")
public class Ingredient implements Serializable {
@Id
@Column(name = "ingredient_id")
private Long id;
@NotEmpty
private String name;
private Integer quantity;
// Getters and Setters
}
Recipe.java
@Entity
@Table(name = "recipes")
public class Recipe implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "recipe_id")
private Long id;
@NotEmpty
private String name;
@ManyToOne(fetch = FetchType.LAZY)
private Client client;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "fk_recipe_id")
private List<Ingredient> ingredients;
// Getters and Setters
}
What I want to do is that the PK (recipes_ingredients table) is composed of: ingredient_id
and recipe_id
in some way (using the FK that I already have and making it also PK or just taking that ID in some other way and making it PK with ingredient_id
).
The ingredient_id
will be numbers that will start at 1 onwards for each recipe, in this way if I have a recipe with recipe_id = 1 and 3 ingredients, my idea is that in the ingredients table it has something like this:
ingredient_id(PK) name quantity recipe_id(PK)
1 Oil 150 1
2 Salt 5 1
3 Flour 500 1
Any idea how I can do this?