Java 没有活动事务Hibernate Spring,CreateQuery无效
您好,当我使用Java 没有活动事务Hibernate Spring,CreateQuery无效,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,您好,当我使用sessionFactory.openSession()时,我遇到了以下问题:它工作正常,但会话会话=this.sessionFactory.getCurrentSession()我得到以下错误 DAO public List<Bank> listbank(String bankId) { Session session = this.sessionFactory.getCurrentSession(); Query query = s
sessionFactory.openSession()时,我遇到了以下问题:代码>它工作正常,但会话会话=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;
}
我的应用程序使用spring运行,使用Jersey运行hibernate
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>
classpath:properties/database.properties
真的
真的
线
应用程序上下文
<?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>
您错过了会话。beginTransaction();在你的代码里。
将其放在Session Session=this.sessionFactory.getCurrentSession()之后 从配置中删除hibernate.current\u session\u context\u class
,并确保您正在运行的方法上有@Transactional
。@Transactional(传播=传播.支持,只读=真)正在服务中layer@M.Deinum已删除休眠。当前会话上下文类,现在我正在获取org.hibernate.hibernate异常:未找到当前线程的会话
,这意味着您的方法上没有@Transactional
。然后您有多个dao实例。。。您可能有一个contextloaderlistener和一个dispatcherservlet以及一个组件扫描元素?
<?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>