org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thre

来源:互联网 发布:overlay 网络 编辑:程序博客网 时间:2024/05/21 11:22

在spring中用到Hibernate,Junit Test 报此错误 ,看错误信息应该联想到可能是缺少@Transactional注解,这是事务管理的注解,在Imp里面,与数据库相关的类,应该加上注解@Repository和@Transactional,同时保证你的xml有配置Hibernate 事务,我的Struts2-xml配置如下(SpringMVC-xml也一样)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsdhttp://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- <context:component-scan base-package="package_name"/> --><context:component-scan base-package="org.lee" /><mvc:annotation-driven /><!-- 数据库配置 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><!-- MySQL数据库配置 --><property name="url"value="jdbc:mysql://localhost:3306/struts2?useUnicode=true&characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="123456" /></bean><!-- 配置JdbcTemplate --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"abstract="false" lazy-init="false" autowire="default"><property name="dataSource"><ref bean="dataSource" /></property></bean><!--配置Hibernate --><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="packagesToScan" value="org.lee.model"></property><property name="hibernateProperties"><props><!-- 方言 --><prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop><!-- 控制台显示SQL --><prop key="show_sql">true</prop><!-- 自动更新表结构 --><prop key="hbm2ddl.auto">update</prop></props></property></bean><!--配置Hibernate事務 --><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>


阅读全文
1 0
原创粉丝点击