cascade deleting...- -
来源:WEB开发资源联盟(http://cnpoint.com/)
作者:phpcms
原文:cascade deleting...- -(http://cnpoint.com/framwwork/2006/1213/content_4734.htm)
cascade deleting...- -
一切进行得很顺利,大家的热情也很高,也许是因为我被我感染了吧。不过还是可以明显地看到老板头上的愁云。

刚才遇到这样一个问题:
在session.delete(customer)的时候,因为定义的层级关系是cascade="all-delete-orphan",当然all在这个情况下也是可以的啦,cascade delete会把所有user和shop包含进去,可是这两个实体是由自身的父子关系的,也就是说我在他们上边实现了这样的接口:
interface Family{
public Set getChildren();
public Object getParent();
public boolean hasChild();
public boolean hasParent();
.....
}
也就是说大概有这样的映射:
<many-to-one name="parent" column="PARENTID" class="Users" />
<set name="children" inverse="true" lazy="true">
<key column="PARENTID"/>
<one-to-many class="Users"/>
</set>
这是我改过以后的,因为parent不可以为空,所以设置了not-null="true",出现not-null property references a null or transient value的错误,
只有放弃对not-null的parent设置。
我很困惑。
reference上说:
-
If the child object's lifespan is bounded by the lifespan of the of the parent object make it a lifecycle object by specifying cascade="all".
-
Otherwise, save() and delete() it explicitly from application code. If you really want to save yourself some extra typing, use cascade="save-update" and explicit delete().
我想,解决方法,有三个:
1、像他说的一样,给出自己写的delete方法;2、去掉not-null限制(我才不会这么做);3、写出Interceptor(我还是等Hibernate3的EventManager吧);
4、其实很简单,哎,想了半天,原来什么事情都有最简单的解决方法。在children上要另外加上至少cascade="delete"的限制,就可以不出现那个异常,而且细想起来,这也是很完备的限制
进入问吧