异常描述:执行以下的addAsHaveParentId2方法出现此异常:
/*-----------------------类Dept、Dept.hbm.xml有parentId属性(数据库中有此列)----------------------*/
@Test//添加一级部门--success(parentId为Null)
public void addAsHaveParentId1(){
Dept d = new Dept("市场部", "湖北襄阳");
deptService.saveOrUpdate(d);
}
@Test//添加二级部门--fail:原因: deptService.findById(88)的parentId为Null
public void addAsHaveParentId2(){
Dept d = deptService.findById(89); Dept d1 = new Dept("市场部一组", "湖北襄阳");
d1.setParent(d);
d.getChilds().add(d1);
Dept d2 = new Dept("市场部二组", "湖北襄阳");
d2.setParent(d);
d.getChilds().add(d2); deptService.saveOrUpdate(d1);//分别添加子部门
deptService.saveOrUpdate(d2); // deptService.saveOrUpdate(d);//也可以只添加上级部门
}
具体异常信息:
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.chen.vo.Dept.parentId
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:109)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:352)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:232)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3580)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:152)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.chen.dao.BaseDaoImp.getObject(BaseDaoImp.java:64)
at com.chen.dao.DeptDaoImpl.findById(DeptDaoImpl.java:31)
at com.chen.service.DeptService.findById(DeptService.java:29)
at com.chen.service.DeptService$$FastClassByCGLIB$$760bf484.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)
at com.chen.service.DeptService$$EnhancerByCGLIB$$4025fc92.findById(<generated>)
at com.chen.test.DeptTest.addAsHaveParentId2(DeptTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66)
... 59 more
分析:在类Dept及其配置文件Dept.hbm.xml均有parentId属性,如下所示:
类Dept:
public class Dept {
private int did;
private String dname;
private String loc;
private int parentId;
//..
}
Dept.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.chen.vo.Dept" table="t_dept">
<id name="did">
<generator class="sequence">
<param name="sequence">SEQ_DEPT</param>
</generator>
</id>
<property name="dname"/>
<property name="loc"/>
<property name="parentId" insert="false" update="false"/>
<!- - 省略.. -->
</class>
</hibernate-mapping>
在执行addAsHaveParentId1方法后,添加了一个一级部门,did为89,其parentId在数据库中为Null(因为在Dept.hbm.xml中设置了insert="false" update="false",所有不能对此字段执行插入/更新操作)。在执行addAsHaveParentId2方法时,因为类Dept有parentId,所有Hibernate的Sesssion中有维护此字段,但对于did为89的部门来说它是Null的,所以抛出异常。注意:parentId在数据库中为Null没有关系,关键是它在Session中管理中也是Null的,Hibernate因此不能在添加部门时级联添加上级部门(准确的解释目前尚不好说)。
解决方法:删去类Dept中的parentId及相应的配置文件中的parentId
测试:正常!
/*-----------------------类Dept、Dept.hbm.xml中无parentId属性(但数据库中有此列)----------------------*/
@Test//添加一级部门--success(parentId为Null)
public void addAsNoParentId1(){
Dept d = new Dept("市场部", "湖北襄阳");
deptService.saveOrUpdate(d);
}
@Test//添加二级部门--success
public void addAsNoParentId2(){
Dept d = deptService.findById(103); Dept d1 = new Dept("市场部一组", "湖北襄阳");
d1.setParent(d);
d.getChilds().add(d1);
Dept d2 = new Dept("市场部二组", "湖北襄阳");
d2.setParent(d);
d.getChilds().add(d2); // deptService.saveOrUpdate(d1);//分别添加子部门
// deptService.saveOrUpdate(d2); deptService.saveOrUpdate(d);//也可以只添加上级部门
}
小结:对于Hibernate自连接的一对多,多对一的情况——如部门的上级部门与下级部门,最好在掉持久类中的不设置parentId字段,否则在添加部门时级联添加已存在的上级部门会出现此异常。