Hibernate JPA merge对引用的集合不做任何操作

Hibernate JPA merge对引用的集合不做任何操作,hibernate,jpa,merge,Hibernate,Jpa,Merge,我有一个recipies应用程序,由3个主要表格组成: -报酬 -接收要素(关联表) -配料 在我的应用程序中,用户必须能够更改配方的成分数量。为了做到这一点,我做了以下几点 public void updateRecipe(final Recipe pRecipe) throws InsertException { deleteRemovedRecipeIngredients(pRecipe); deleteRemovedRecipeSteps(pRecipe); En

我有一个recipies应用程序,由3个主要表格组成: -报酬 -接收要素(关联表) -配料

在我的应用程序中,用户必须能够更改配方的成分数量。为了做到这一点,我做了以下几点

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”类的更新,而对“RecipeIngCredit”(包含数量)没有任何更新

在这里您可以找到以下类:

--------------------------------------配方类------------------------------

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

}
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());
}

}

RecipeCredit是不可变的,这意味着它不会被更新


谢谢,现在我确实有一个关于RecipeCredit的sql语句,但我有一个新的错误警告[org.hibernate.engine.jdbc.spi.SqlExceptionHelper](默认任务-25)sql错误:0,SQLState:08S01错误[org.hibernate.engine.jdbc.spi.SqlExceptionHelper](默认任务-25)通信链路故障,我的“MySQLServer”服务停止,知道吗?看来你和数据库的连接有问题。相关的