Hibernate OrphanRemoval仅从关系表中删除数据

时间:2014-12-07 04:28:03

标签: java spring hibernate jsp

我有一个RECIPE表与INGREDIENT表具有OneToMany关系,因为单个配方可以有很多成分。问题是如果用户删除了一个成分(通过前端将所有字段(ingredient_idingredient)设置为NULL,则删除包含两个表RECIPE_INGREDIENT的关系的行但Ingredient表中的行仍然存在。我们不能告诉Hibernate还删除那些行吗?

Oracle表格

create table recipe(id number primary key,
                    name varchar2(25) unique);

create table ingredient(ingredient_id number(4) primary key,
                    ingredient varchar2(40));

create table recipe_ingredient(recipe_id number(4),
                           ingredient_id number(4),
                           constraint recipe_fk foreign key(recipe_id)
                           references recipe(recipe_id),
                           constraint ingredient_fk foreign
                           key(ingredient_id) references
                           ingredient(ingredient_id));

成分和配方POJO

@Entity
@Table(name = "ingredient", uniqueConstraints={
        @UniqueConstraint(columnNames="INGREDIENT_ID")
})
public class Ingredient implements Serializable {   
    @Id
    @Column(name = "INGREDIENT_ID", unique=true, nullable=false)
    @SequenceGenerator(name="seq_ingredient", sequenceName="seq_ingredient")
    @GeneratedValue(strategy=GenerationType.AUTO, generator="seq_ingredient")
    private Integer ingredientId;

    @Column(name = "INGREDIENT")
    private String ingredient;

    /*@ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="RECIPE_ID")
    private Recipe recipe;*/

    //getter and setters



@Entity
@Table(name = "recipe")
public class Recipe implements Serializable {    
    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinTable(name = "recipe_ingredient", joinColumns = { @JoinColumn(name = "recipe_id") }, inverseJoinColumns = { @JoinColumn(name = "ingredient_id") })
    private List<Ingredient> ingredients;

//getters and setter
}

DAO代码

public class RecipeDaoImpl implements RecipeDao {
    public void addRecipe(Recipe recipe) {
        getSession().saveOrUpdate(recipe);
    }
}

日志显示INGREDIENT表中的行仍然存在,而Hibernate只是从'RECIPE_INGREDIENT'表中删除行。

请注意以下ingredient_id已删除null。在这两种情况下,它都将ingredient.recipe_id更新为NULL。

Received following from frontend:
RecipeController - Recipe[recipeId=126,name=Sandwich,ingredients=[Ingredient[ingredientId=270,ingredient=Salt],[ingredientId=<null>,quantity=<null>]]]

Hibernate: update RECIPE set NAME=? where RECIPE_ID=?
Hibernate: update ingredient set INGREDIENT=? where INGREDIENT_ID=?
Hibernate: delete from recipe_ingredient where recipe_id=?
Hibernate: insert into recipe_ingredient (recipe_id, ingredient_id) values (?, ?)

所以数据库表有,

INDREDIENT
INGREDIENT_ID   INGREDIENT
271             Salt    
272             Sugar

RECIPE_INDGREDIENT
RECIPE_ID   INDREDIENT_ID
126         271

1 个答案:

答案 0 :(得分:0)

您是否在Receipe和Indgredient类中实现了equals()和hashcode()方法正确?如果没有那么那可能是为什么indgredient表中的行不被删除的原因。有关详情,请参阅this文章。