【发布时间】:2012-01-21 21:08:52
【问题描述】:
我遇到了休眠问题。
简而言之:
当关系有属性并且需要级联保存、删除和更新时,如何配置与Hibernate的ManyToMany关联?
大尺寸:
想象以下数据库:
Super Mini
M______N
|
attribute
这里有3张桌子:
"Mini", "Super" and "Super_Mini".
现在想象 Super_Mini 有 1 个关系属性(显然还有键)。
好的,现在通过以下方式将其转换为 Hibernate:
Super:
// The relation is Many to Many, but considering that it has an attribute, this is OneToMany with the ManyMany RelationShip
@OneToMany(mappedBy="mini", targetEntity=Mini.class)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
@LazyCollection(LazyCollectionOption.TRUE)
private Set<SuperMini> superMini = new HashSet<SuperMini>();
SuperMini:
@Id
@ManyToOne(targetEntity=Super.class,fetch=FetchType.LAZY)
@Cascade({CascadeType.LOCK})
@JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") })
private Super super;
@Id
@ManyToOne(targetEntity=Mini.class,fetch=FetchType.LAZY)
@Cascade({CascadeType.LOCK})
@JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") })
private Mini mini;
所以,我认为配置是正确的,并且如果对象有Mini childrens,则独立保存所有这些。问题是当我尝试删除对象时:
Super data = getHibernateTemplate().load(Super.class, idSuper);
getHibernateTemplate().getSessionFactory().getCurrentSession().clear();
data.setMini( new HashSet<Mini>() );
getHibernateTemplate().delete( data );
getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
Hibernate 不删除 Mini 关系...有什么问题?我知道如何通过HQL解决,但可能配置不正确,我不知道。
提前谢谢你,
【问题讨论】:
标签: java hibernate annotations cascade cascading-deletes