【问题标题】:JPA merge does nothing to referenced collectionJPA 合并对引用的集合没有任何作用
【发布时间】:2016-06-02 07:54:52
【问题描述】:

我有一个食谱应用程序,由 3 个主表组成: - 食谱 -RecipeIngredients(关联表) -成分

在我的应用程序中,用户必须能够更改食谱的成分数量。为此,我执行以下操作

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 类--------- -------------

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

}

【问题讨论】:

    标签: hibernate jpa merge


    【解决方案1】:

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

    Hibernate Docs

    【讨论】:

    • 谢谢,现在我确实有一个关于RecipeIngredient 的sql 语句,但是我有一个新错误WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-25) SQL 错误: 0, SQLState: 08S01 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-25) Communications link failure 我的“MySQLServer”服务宕机了,知道吗?
    • 您与 db 的连接似乎有问题。stackoverflow.com/a/16724989/5047727 相关
    猜你喜欢
    • 2015-07-11
    • 2014-05-28
    • 2011-03-13
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多