簡體   English   中英

沒有活動事務休眠彈簧,CreateQuery無效

[英]CreateQuery is not valid without active transaction Hibernate Spring

您好,我在使用sessionFactory.openSession();sessionFactory.openSession();以下問題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 

我的應用程序在春季運行,並在Jersey休眠

Databaseconfig.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>

的ApplicationContext

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

您錯過了session.beginTransaction(); 在我們的代碼中。 將其放置在Session session = this.sessionFactory.getCurrentSession();之后;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM