我有一个收件人应用程序,包含3个主表: -Recipies -RecipeIngredients(关联表) -Ingredients
在我的应用程序中,用户必须能够更改配方的配料数量。为了做到这一点,我做了以下
public void updateRecipe(final Recipe pRecipe) throws InsertException {
deleteRemovedRecipeIngredients(pRecipe);
deleteRemovedRecipeSteps(pRecipe);
EntityManager entityManager = HibernateUtilities.getEntityManager();
entityManager.getTransaction().begin();
try {
Recipe recipe = new Recipe();
recipe.setRecipeName(pRecipe.getRecipeName());
recipe.setProteinPerHundredGrams(pRecipe.getProteinPerHundredGrams());
recipe.setCarbsPerHundredGrams(pRecipe.getCarbsPerHundredGrams());
recipe.setFatPerHundredGrams(pRecipe.getFatPerHundredGrams());
recipe.setRecipeImg(pRecipe.getRecipeImg());
recipe.setTotalWeight(pRecipe.getTotalWeight());
recipe.setRecipeId(pRecipe.getRecipeId());
entityManager.merge(recipe);
for (RecipeIngredient recipeIngredient : pRecipe.getRecipeIngredients()) {
RecipeIngredient rp = new RecipeIngredient(recipe, recipeIngredient.getIngredient(),
recipeIngredient.getIngredientQuantity(), recipeIngredient.getQuantityUnit());
entityManager.merge(rp);
}
for (RecipeStep recipeStep : pRecipe.getRecipeSteps()) {
RecipeStep rs = new RecipeStep(recipeStep.getStepNumber(), recipe, recipeStep.getStepDescription());
entityManager.merge(rs);
}
} catch (Exception e) {
entityManager.getTransaction().rollback();
e.printStackTrace();
throw new InsertException("Could not add recipe");
}
entityManager.getTransaction().commit();
entityManager.clear();
}
但是生成的SQL查询只是对" Recipe"的更新。课堂上没有任何关于" RecipeIngredient" (包含数量)。
您可以在这里找到课程:
--------------------------------------食谱类------- -----------------------
package com.tnidjra.data.entities;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "recipes")
public class Recipe implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "RECIPE_ID")
private int recipeId;
@Column(name = "RECIPE_NAME")
private String recipeName;
@OneToMany(mappedBy = "recipe", fetch=FetchType.EAGER)
private Set<RecipeIngredient> recipeIngredients = new HashSet<RecipeIngredient>();
@OneToMany(mappedBy = "recipe", fetch=FetchType.EAGER)
private Set<RecipeStep> recipeSteps = new HashSet<RecipeStep>();
@Column(name = "PROTEIN_PER_HUNDRED_GRAMS")
private double proteinPerHundredGrams;
@Column(name = "FAT_PER_HUNDRED_GRAMS")
private double fatPerHundredGrams;
@Column(name = "CARB_PER_HUNDRED_GRAMS")
private double carbsPerHundredGrams;
@Column(name = "RECIPE_IMG")
private String recipeImg;
@Column(name = "TOTAL_WEIGHT")
private double totalWeight;
public int getRecipeId() {
return recipeId;
}
public void setRecipeId(int recipeId) {
this.recipeId = recipeId;
}
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public Set<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(Set<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public Set<RecipeStep> getRecipeSteps() {
return recipeSteps;
}
public void setRecipeSteps(Set<RecipeStep> recipeSteps) {
this.recipeSteps = recipeSteps;
}
public double getProteinPerHundredGrams() {
return proteinPerHundredGrams;
}
public void setProteinPerHundredGrams(double proteinPerHundredGrams) {
this.proteinPerHundredGrams = proteinPerHundredGrams;
}
public double getFatPerHundredGrams() {
return fatPerHundredGrams;
}
public void setFatPerHundredGrams(double fatPerHundredGrams) {
this.fatPerHundredGrams = fatPerHundredGrams;
}
public double getCarbsPerHundredGrams() {
return carbsPerHundredGrams;
}
public void setCarbsPerHundredGrams(double carbsPerHundredGrams) {
this.carbsPerHundredGrams = carbsPerHundredGrams;
}
public String getRecipeImg() {
return recipeImg;
}
public void setRecipeImg(String recipeImg) {
this.recipeImg = recipeImg;
}
public double getTotalWeight() {
return totalWeight;
}
public void setTotalWeight(double totalWeight) {
this.totalWeight = totalWeight;
}
}
------------------------------------ RecipeIngredient class --------- -------------
package com.tnidjra.data.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "recipe_ingredients")
@org.hibernate.annotations.Immutable
public class RecipeIngredient implements Serializable {
@Embeddable
public static class Id implements Serializable {
@Column(name = "RECIPE_ID")
protected Long recipeId;
@Column(name = "INGREDIENT_ID")
protected Long ingredientId;
public Id() {
}
public Id(Long pRecipeId, Long pIngredientId) {
this.recipeId = pRecipeId;
this.ingredientId = pIngredientId;
}
public boolean equals(Object o) {
if (o != null && o instanceof Id) {
Id that = (Id) o;
return this.recipeId.equals(that.recipeId)
&& this.ingredientId.equals(that.ingredientId);
}
return false;
}
public int hashCode() {
return recipeId.hashCode() + ingredientId.hashCode();
}
}
@EmbeddedId
protected Id id = new Id();
@ManyToOne
@JoinColumn(name = "RECIPE_ID", insertable = false, updatable = false)
private Recipe recipe;
@ManyToOne
@JoinColumn(name = "INGREDIENT_ID", insertable = false, updatable = false)
private Ingredient ingredient;
@Column(name = "INGREDIENT_QUANTITY")
private int ingredientQuantity;
@ManyToOne
@JoinColumn(name = "QUANTITY_UNIT")
private Unit quantityUnit;
public RecipeIngredient() {
}
public RecipeIngredient(final Recipe pRecipe, final Ingredient pIngredient, int pIngredientQuantity, final Unit pQuantityUnit) {
this.recipe = pRecipe;
this.ingredient = pIngredient;
this.ingredientQuantity = pIngredientQuantity;
this.quantityUnit = pQuantityUnit;
this.id.recipeId = (long) pRecipe.getRecipeId();
this.id.ingredientId = (long) pIngredient.getIngredientId();
recipe.getRecipeIngredients().add(this);
}
public Recipe getRecipe() {
return recipe;
}
public void setRecipe(Recipe recipe) {
this.recipe = recipe;
}
public Ingredient getIngredient() {
return ingredient;
}
public void setIngredient(Ingredient ingredient) {
this.ingredient = ingredient;
}
public int getIngredientQuantity() {
return ingredientQuantity;
}
public void setIngredientQuantity(int ingredientQuantity) {
this.ingredientQuantity = ingredientQuantity;
}
public Unit getQuantityUnit() {
return quantityUnit;
}
public void setQuantityUnit(Unit unit) {
this.quantityUnit = unit;
}
@Override
public boolean equals(Object o) {
if (o != null && o instanceof RecipeIngredient) {
RecipeIngredient that = (RecipeIngredient) o;
return this.id.equals(that.id);
}
return false;
}
@Override
public int hashCode() {
return (int) (this.recipe.getRecipeId() + this.ingredient.getIngredientId());
}
}