【问题标题】:CreateQuery is not valid without active transaction Hibernate Spring如果没有活动事务 Hibernate Spring,CreateQuery 无效
【发布时间】:2017-03-23 11:53:32
【问题描述】:

您好,我遇到以下问题,当我使用 sessionFactory.openSession(); 时它工作正常但 Session session = this.sessionFactory.getCurrentSession(); 我收到以下错误

DAO

public List<Bank> listbank(String bankId) {

        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Bank ");
        List<Bank>bankList =query.list();
        session.close();
        return bankList;
    }

错误

org.hibernate.HibernateException: createQuery is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    at com.sun.proxy.$Proxy86.createQuery(Unknown Source)
    at 

我的应用程序使用 spring 运行并使用 Jersey 进行休眠

数据库配置.xml

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

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:properties/database.properties</value>
        </property>
    </bean>

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

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
            </props>
        </property>
    </bean>

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

    <tx:annotation-driven/> 



</beans>

应用程序上下文

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


    xmlns:aop="http://www.springframework.org/schema/aop"
    >   

    <!-- for database configuration -->
    <import resource="classpath*:/config/db-configuration.xml" />

    <!-- for spring-security-with-oauth configuration -->
    <import resource="classpath*:/config/spring-security-oauth2.xml" />

    <context:component-scan base-package="com" />

    <context:annotation-config></context:annotation-config>



</beans>

【问题讨论】:

  • 从您的配置中删除hibernate.current_session_context_class,并确保您正在运行的方法上有@Transactional
  • @Transactional(propagation=Propagation.SUPPORTS,readOnly=true) 在服务层
  • @M.Deinum 删除了hibernate.current_session_context_class,现在我得到了org.hibernate.HibernateException: No Session found for current thread
  • 这意味着您的方法中没有@Transactional
  • 那么您有多个 dao 实例...您可能同时拥有一个带有组件扫描元素的 contextloaderlistener 和 dispatcherservlet?

标签: java spring hibernate spring-mvc


【解决方案1】:

你错过了 session.beginTransaction();在你的代码中。 将其放在 Session session = this.sessionFactory.getCurrentSession();

之后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2013-12-16
    • 2023-04-05
    • 2015-10-19
    • 1970-01-01
    • 2021-12-09
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多