当前位置:编程学习 > JAVA >>

Spring3.1整合Hibnerte4.1无法获取session错误,求指教!

spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化时获取的连接数 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<!-- 连接池中保留的最大连接数。 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<!-- 每60秒检查所有连接池中的空闲连接。 -->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="c3p0dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
<!-- 数据库方言 oracle11g 仍然采用10g的 org.hibernate.dialect.Oracle10gDialect -->
<!-- 目前采用mysql数据库,以后采用oralce11g -->
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</prop>
<prop key="hibernate.ehcache_config_file">com/avicit/resource/ehcache.xml</prop>
</props>
</property>
<property name="packagesToScan" value="com.avicit.fes.**.domain" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<tx:method name="do*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.avicit..service..*.*(..))"/>
</aop:config>

<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>

service中的方法:

@Autowired
private GeneralHibernateDao hibernateDao;

@Override
public void selectSimple() {
logger.debug(hibernateDao);
hibernateDao.find(SimpleController.class);
}

DAO中的方法

public abstract class AbstractHibernateDao<T, ID> implements
HibernateDao<T, ID> {

@Autowired
private SessionFactory sessionFactory;

protected Session getSession() {
return sessionFactory.getCurrentSession();
}
}
public class GeneralHibernateDao extends AbstractHibernateDao {


public Object get(Class clazz,Serializable id){
return getSession().get(clazz, id);
}

public List<?> find(Class clazz,Object... params){
return this.findPageByHql(buildHQLByClass(clazz), params);
}
}

调用出现的错误信息:

org.hibernate.HibernateException: createQuery is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
at $Proxy14.createQuery(Unknown Source)
at com.avicit.framework.support.dao.impl.AbstractHibernateDao.createHQLQuery(AbstractHibernateDao.java:121)
at com.avicit.framework.support.dao.impl.AbstractHibernateDao.findPageByHql(AbstractHibernateDao.java:67)
at com.avicit.framework.support.dao.impl.GeneralHibernateDao.find(GeneralHibernateDao.java:19)
at com.avicit.fes.system.易做图.service.impl.SimpleServiceImpl.selectSimple(SimpleServiceImpl.java:23)
at com.avicit.fes.system.易做图.controller.SimpleController.index(SimpleController.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
--------------------编程问答--------------------

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

我更改了这项配置,还是报错,错误如下:

严重: Servlet.service() for servlet spring-dispather threw exception
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at com.avicit.framework.support.dao.impl.AbstractHibernateDao.getSession(AbstractHibernateDao.java:24)
at com.avicit.framework.support.dao.impl.AbstractHibernateDao.createHQLQuery(AbstractHibernateDao.java:121)
at com.avicit.framework.support.dao.impl.AbstractHibernateDao.findPageByHql(AbstractHibernateDao.java:67)
at com.avicit.framework.support.dao.impl.GeneralHibernateDao.find(GeneralHibernateDao.java:19)
at com.avicit.fes.system.易做图.service.impl.SimpleServiceImpl.selectSimple(SimpleServiceImpl.java:23)
at com.avicit.fes.system.易做图.controller.SimpleController.index(SimpleController.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)


求各位大侠赐教! --------------------编程问答-------------------- 大哥我也在找这个问题,同求! --------------------编程问答-------------------- 路过帮顶,hibernate都出到4了啊 --------------------编程问答-------------------- mark..我也遇到这个问题了 --------------------编程问答-------------------- 在Dao里应该有一个sessionFactory的set方法吧。
我是用的是javax.annotation.Resource的annotation。
在Spring配置文件中transactionManager声明下面annotation的配置如下:
<tx:annotation-driven transaction-manager="transactionManager" />


之后在Dao里这样获取sessionFactory:

private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}


希望对你有帮助 --------------------编程问答-------------------- hibernateDao没有注入进去啊,没有hibernateDao这样的bean啊 --------------------编程问答-------------------- @Component
public class GeneralHibernateDao extends AbstractHibernateDao {


    public Object get(Class clazz,Serializable id){
        return getSession().get(clazz, id);
    }
    
    public List<?> find(Class clazz,Object... params){
        return this.findPageByHql(buildHQLByClass(clazz), params);
    }
}
加注解 --------------------编程问答-------------------- service 的实现类加了@Component 还是一样报错org.hibernate.HibernateException: No Session found for current thread  啊…… --------------------编程问答-------------------- 我也是这个问题,求教啊  --------------------编程问答-------------------- 帮顶,请教.... --------------------编程问答-------------------- 加上这个试试
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
--------------------编程问答--------------------
引用 4 楼 nijimin100 的回复:
mark..我也遇到这个问题了

易做图 ,我没遇到这个问题 --------------------编程问答-------------------- service层缺少一个注解,或者在spring的配置文件中加一个aop的配置,楼主在service类上面加上一个这个试试@transaction --------------------编程问答-------------------- 这是涉及到一个open session in view的东东 --------------------编程问答-------------------- 之前也遇上这个问题,执行的类要和 切入点一致才行 --------------------编程问答-------------------- 好久没用过ssh了,帮顶 --------------------编程问答-------------------- 我也遇到这个问题,看看这个吧:
http://blog.csdn.net/wzk527/article/details/8543480
--------------------编程问答-------------------- spring3 hibernate4 整合 所有方法必须开启事物。。
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,