当前位置: 代码迷 >> Java Web开发 >> Hibernate4+spring3.1集成有关问题
  详细解决方案

Hibernate4+spring3.1集成有关问题

热度:3260   发布时间:2013-02-25 21:15:27.0
Hibernate4+spring3.1集成问题
XML code
    <bean id="framework.support.hibernate.sessionFactory"        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"        lazy-init="false">        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">${framework.hibernate.dialect}</prop>                <prop key="hibernate.hbm2ddl.auto">${framework.hibernate.hbm2ddl.auto}</prop>                <prop key="hibernate.show_sql">${framework.hibernate.show_sql}</prop>                <prop key="hibernate.jdbc.batch_size">30</prop>                <prop key="hibernate.order_inserts">true</prop>                <prop key="hibernate.order_updates">true</prop>                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext                </prop>                <!-- cache -->                <prop key="net.sf.ehcache.configurationResourceName">${framework.net.sf.ehcache.configurationResourceName}</prop>                <prop key="hibernate.cache.use_second_level_cache">${framework.hibernate.cache.use_second_level_cache}</prop>                  <prop key="hibernate.cache.use_query_cache">${framework.hibernate.cache.use_query_cache}</prop>                <prop key="hibernate.cache.provider_class">${framework.hibernate.cache.provider_class}</prop>                  <prop key="hibernate.cache.region.factory_class">${framework.hibernate.cache.region.factory_class}</prop>            </props>        </property>        <property name="dataSource" ref="framework.support.dataSource" />        <property name="mappingDirectoryLocations">            <list>                <value>classpath:com/hzyl</value>            </list>        </property>    </bean>


我在sping 配置
<bean id="sampleService" class="com.hzyl.sample.service.SampleService" init-method="init" />
init 方法会查询数据库,sessionFactory.getCurrentSession();的时候会抛出一下异常,
但如果不用init 方法,等程序启动完毕之后再查询数据库就不会

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.hzyl.framework.orm.hibernate.HibernateEntityManager.getSession(HibernateEntityManager.java:60)
at com.hzyl.framework.orm.hibernate.HibernateEntityManager.find(HibernateEntityManager.java:143)
at com.hzyl.sample.service.SampleService.init(SampleService.java:17)
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)

------解决方案--------------------------------------------------------
很久没弄这个了,先帮你顶一下
  相关解决方案