[英]Hibernate 2 primary keys (1 is a foreign key)
My question is about the code below:我的问题是关于下面的代码:
Ingredient.java成分.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配方.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
).我想要做的是PK ( recipes_ingredients表)由以下ingredient_id
组成: 以某种方式(使用我已经拥有的FK并将recipe_id
PK或仅以其他方式获取该 ID 并使其与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
ID 将是每个食谱从 1 开始的数字,这样如果我有一个食谱 ID = 1 和 3 成分的食谱,我的想法是在成分表中它有这样的内容:
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?知道我该怎么做吗?
public class IngredientId {
private Recipe recipe;
private Long id;
public IngredientId(Recipe recipe, Long id) {
this.recipe = recipe;
this.id = id;
}
public IngredientId() {
}
//Getters and setters are omitted for brevity
public Recipe getRecipe() {
return recipe;
}
public void setRecipe(Recipe recipe) {
this.recipe = recipe;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
IngredientId ingredientId = (IngredientId) o;
return Objects.equals( recipe, ingredientId.recipe ) && Objects.equals( id, ingredientId.id );
}
@Override
public int hashCode() {
return Objects.hash( recipe, id );
}
}
@Entity
@IdClass( IngredientId.class )
@Table(name = "recipes_ingredients")
public class Ingredient {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "recipe_id")
private Recipe recipe;
@Id
@GeneratedValue
@Column(name = "ingredient_id")
private Long id;
private String name;
private Integer quantity;
public IngredientId getId() {
return new IngredientId( recipe, id );
}
public void setId(IngredientId id) {
this.recipe = id.getRecipe();
this.id = id.getId();
}
}
@Entity
@Table(name = "recipes")
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "recipe_id")
private Long id;
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "recipe_id")
private List<Ingredient> ingredients = new ArrayList<>();
}
It's important to notice that a composite key cannot have null values.请务必注意,复合键不能具有 null 值。 So this makes sense only if you know that each ingredient is bound to a recipe.因此,只有当您知道每种成分都与食谱绑定时,这才有意义。 See this answer on StackOverflow and this Hibernate Jira 在 StackOverflow和这个 Hibernate Jira上查看这个答案
You can have a look at the Hibernate ORM documentation for more details.您可以查看Hibernate ORM 文档了解更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.