如果想通过级联属性删除一端的数据和多端的数据要使用 void org.hibernate.Session.delete(Object arg0) 方法。

getSession().delete(team);//使用级联属性删除数据可以删除成功。

如果使用HQL语句,使用executeUpdate()方法删除则会抛出异常。

        Integer id  = team.getId();
String hql = "DELTE FROM Team t WHERE t.id = ?"; getSession().createQuery(hql).setInteger(0, id).executeUpdate();//这样进行级联删除会失败。

以上删除会抛出异常,如下:

JUnit 抛出的异常

java.lang.IllegalArgumentException: node to traverse cannot be null!
at org.hibernate.hql.internal.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:63)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:272)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at com.zcd.hibernate.oneToMany.TeamDao.delete(TeamDao.java:32)
at com.zcd.hibernate.service.TestService.deleteTeam(TestService.java:111)
at com.zcd.hibernate.service.TestService$$FastClassBySpringCGLIB$$5532ace.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint

05-11 11:15