2015-06-01 17 views
1

我有一些实体(WarehouseTransactionGroup,WarehouseTransactionAttribute,的AttributeValue)有下列关系的母公司:Hibernate不会删除父但是移除儿童(或关系),当它不能删除,因为外键约束

@Entity 
public class WarehouseTransactionGroup extends MasterEntity { 

    @ManyToMany(cascade = CascadeType.ALL) 
    @MapKeyColumn 
    private Map<WarehouseTransactionAttribute, AttributeValue> attributes; 

    // Getter and setter 

} 

当我尝试删除WarehouseTransactionGroup类型的对象,由于外键约束,它自然会失败,因为该对象在其他数据库记录中使用。但是,Hibernate会清除属性字段中的数据。

我正在调用一个事务中的remove操作,即session.delete()。

为什么会发生这种情况,我该如何解决?

回答

0

shouldn't use CascadeType.ALL for @ManyToMany协会。

  1. 您需要级联更改为CascadeType.PERSIST, CascadeType.MERGE

    @ManyToMany(级联= {CascadeType.PERSIST,CascadeType.MERGE}) @MapKeyColumn 私人地图属性;

  2. 的删除是这样的:

    WarehouseTransactionGroup wtg = ...; 
    attributes.clear(); 
    em.remove(wtg);