断点调试代码走到Dao后报空指针异常,在Dao层的代码比较简单就在方法里一句:
return this.findListBySqlId("findSqlByMap", paraMap);
然后把框架封装的代码提取出来
public List findListBySqlId(String sqlId, Map, ?> paraMap)
/* */ {
/* 195 */ List list = null;
System.out.println(getSession());
/* */ try {
/* 197 */ String hql = getSqlStatementById(sqlId, paraMap);
/* 198 */ if (StringUtils.isNotBlank(hql)) {
/* 199 */ Query query = getSession().createSQLQuery(hql);
/* 200 */ query.setCacheable(SysPropertiesUtil.getBoolean("use_query_cache", false));
/* 201 */ setQueryParameters(query, paraMap);
/* 202 */ list = query.list();
/* */ } else {
/* 204 */ throw new BasalException(BasalException.ERROR, "未在 SqlMap配置文件中配置, ID为" + sqlId +
/* 205 */ "hql 语句");
/* */ }
/* */ } catch (Exception e) {
/* 208 */ throw new BasalException(BasalException.ERROR, e.getCause().getMessage(), e.getCause());
/* */ }
/* 210 */ return list;
}
代码走到Query query = getSession().createSQLQuery(hql);后就被捕获异常,报NullPointException。
怀疑是getSession()没有获取到session,所以报空指针,在这行代码前面加入System.out.println(getSession());再次测试。
果然控制台报:
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
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.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
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:644)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:534)
这样问题就比较清晰了,是由于Hibernate中无法获取到spring管理的session对象。
一般可以通过注解和非注解的方式进行事务配置。
Spring整合Hibernate配置
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
${db.Dialect.prefix}.${db.Dialect.suffix}
true '1', false '0'
false
false
true
true
true
update
true
org.springframework.orm.hibernate4.SpringSessionContext
false
true
true
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
org.hibernate.cache.EhCacheProvider
key="hibernate.cache.provider_configuration_file_resource_path">
classpath*:/ehcache-hibernate.xml
com.test.*.ntity
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
PROPAGATION_REQUIRED
然后在Service中的方法上添加@Transactional注解