org.hibernate.LazyInitializationException: could not initialize proxy [com.jiuyisoft.organization.domain.Person#2002056] - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
at com.jiuyisoft.organization.domain.Person$HibernateProxy$aWmYPF6I.getCode(Unknown Source)
at com.jiuyisoft.organization.service.mapper.PersonMapperImpl.entitySuperiorCode(PersonMapperImpl.java:242)
at com.jiuyisoft.organization.service.mapper.PersonMapperImpl.toDto(PersonMapperImpl.java:69)
at com.jiuyisoft.organization.service.mapper.PersonMapperImpl.toDto(PersonMapperImpl.java:17)
at com.jiuyisoft.core.base.service.impl.BaseServiceImpl.toDto(BaseServiceImpl.java:184)
at com.jiuyisoft.core.base.service.impl.BaseServiceImpl$$FastClassBySpringCGLIB$$d71722a8.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.jiuyisoft.organization.service.impl.PersonServiceImpl$$EnhancerBySpringCGLIB$$518679c2.toDto(<generated>)
at com.jiuyisoft.externalInterface.oa.impl.PersonIncrementFromOaToCrmServiceImpl.sync(PersonIncrementFromOaToCrmServiceImpl.java:143)
at com.jiuyisoft.schedule.SyncIncrementPerson.syncSchedule(SyncIncrementPerson.java:69)
at com.jiuyisoft.schedule.SyncIncrementPerson.execute(SyncIncrementPerson.java:50)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
user = userRepository.findOne(person.getUser().getId());
person是一个查询出来的对象,User是person的一个关联对象,为了优化系统性能,设置了懒加载。在通过定时任务"Job"调用Person的查询方法后,hibernate把Session立刻关闭了,所以在后面getUser()的时候就报no session。百度了一些方案,大多都是建议去除或者关闭懒加载,但是我不希望牺牲系统的性能,而且Person与User这两个类被封装起来了,并无修改权限,所以去除懒加载的方案不是我想要的。
想来也不难,既然hibernate为了保护数据将Session及时关闭,那我们可以把整个程序块设置为事务让它挂起啊。所以在定时任务的执行方法上加上@Transactional注解就可以了。
(@Transactional注解有两个包,我们引入Spring事务的包org.springframework.transaction.annotation.Transactional;)