maple_end
maple_end
2017-11-28 08:56

No Hibernate Session bound to thread

  • spring
  • hibernate

尝试用spring+spring mvc+hibernate搭一个简单的项目,最终数据展示的时候hibernate一直报No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here] with root cause的错误,网上找了好多都是说没有配置

 <tx:annotation-driven transaction-manager="transactionManager"/>

但是我是有配置的,非常崩溃,请教下大家。
sessionFactory的配置如下:

    <!-- 配置session工厂 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="defaultDataSource" />
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:/com/recommend/effect/**/model/*.hbm.xml</value>
            </list>
        </property>


        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->

                <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            </props>
        </property>
    </bean>

事务配置如下:

    <!-- 配置事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

     <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <tx:advice transaction-manager="transactionManager" id="defaultTransactionAdvice">
        <tx:attributes>
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="query*" propagation="REQUIRED" read-only="true" />
            <tx:method name="check*" propagation="REQUIRED" read-only="true" />
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

aop控制如下:

    <!-- 服务AOP配置 -->
    <aop:config expose-proxy="true">
        <aop:pointcut id="systemTransactionPolicy"
            expression="execution(* org.trustel.service.hibernate.*.*(..))"/>
        <aop:advisor id="systemTransactionAdvisor" advice-ref="defaultTransactionAdvice"
            pointcut-ref="systemTransactionPolicy" />
    </aop:config>
    <aop:config>
        <aop:pointcut id="developFlowTransactionPolicy"
            expression="execution(* com.recommend.*.service..*.*(..))" />
        <aop:advisor id="developFlowTransactionAdvisor"
            advice-ref="defaultTransactionAdvice" pointcut-ref="developFlowTransactionPolicy" />
    </aop:config>

文件数如下:
图片说明

报错也就是获取currentSession

    protected Query createQuery(String hql) {
        Session session = getSessionFactory().getCurrentSession();
        Query query = session.createQuery(hql);
        return query;
    }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答