<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)

解决方案 »

  1.   

    求高手解答,为什么还会抛出session 没有在当前线程中
      

  2.   

    <bean id="framework.support.hibernate.transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="framework.support.hibernate.sessionFactory" />
    </bean> <aop:config proxy-target-class="true">
        <aop:pointcut id="productServiceMethods"
                    expression="execution(* com.hzyl..*Service.*(..))"/>
        <aop:advisor advice-ref="framework.support.hibernate.transactionAdvice" pointcut-ref="productServiceMethods"/>
      </aop:config>  <tx:advice id="framework.support.hibernate.transactionAdvice" transaction-manager="framework.support.hibernate.transactionManager">
        <tx:attributes>
          <tx:method name="persist*" read-only="false" propagation="REQUIRED" />
    <tx:method name="delete*" read-only="false" propagation="REQUIRED" />
    <tx:method name="import*" read-only="false" propagation="REQUIRED" />
    <tx:method name="save*" read-only="false" propagation="REQUIRED" />
    <tx:method name="find*" read-only="true" propagation="REQUIRED" />
    <tx:method name="get*" read-only="true" propagation="REQUIRED" />
    <tx:method name="list*"  read-only="true" propagation="REQUIRED"/>
    <tx:method name="count*"  read-only="true" propagation="REQUIRED"/>
    <tx:method name="init*"  read-only="true" propagation="REQUIRED"/>
          <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
      </tx:advice>
    在线等 求解
      

  3.   


    似乎是不符合 execution(* com.hzyl..*Service.*(..)) 设置