spring2.5 + hibernate3.2 annotation 配置事务时出错!不知道是不是配置文件错了,大家帮帮忙啊!
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<!-- <property name="driverClass" value="com.mysql.jdbc.Driver"/> -->
<property name="driverClass"
value="oracle.jdbc.driver.OracleDriver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl"
value="jdbc:oracle:thin:@192.168.1.6:1521:test6" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="gachina" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="gachina" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="100" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="5" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!--定义了Hibernate的SessionFactory -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> -->
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop>-->
<!-- <prop key="hibernate.jdbc.batch_size">20</prop> -->
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!--
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
-->
<!-- 绑定会话到当前线程 -->
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<!-- 注解 -->
<property name="annotatedClasses">
<list>
<!-- <value>com.sunray.cpv.db.vo.PersonInfo</value> -->
<value>com.sunray.cpv.db.vo.TbPersonInfo</value>
<value>com.sunray.cpv.db.vo.TbDepartmentInfo</value>
</list>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- annotation 事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="*">
<!-- <context:include-filter/> -->
<!-- <context:exclude-filter/> -->
</context:component-scan>
</beans>
DAO
===>
package com.sunray.cpv.db.dao;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sunray.cpv.db.IDepartmentInfoDAO;
import com.sunray.cpv.db.vo.TbDepartmentInfo;
/**
*
* TbDepartmentInfoDAO 机关信息
*
* 所有的事务的开启放到业务层,在DAO层不做事务控制
*
* @author lxl
*
*/
@Repository("TbDepartmentInfoDAO")
public class TbDepartmentInfoDAO{
private SessionFactory sessionFactory;
@Autowired(required=true)
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public TbDepartmentInfo getDepartmentInfoByCode(String vcdepartmentcode) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from TbDepartmentInfo d where d.vcdepartmentcode = :vcdepartmentcode");
query.setString("vcdepartmentcode", vcdepartmentcode);
List list = query.list();
if(list != null && list.size() > 0){
TbDepartmentInfo department = (TbDepartmentInfo)list.get(0);
return department;
}
return null;
}
@Transactional
public TbDepartmentInfo saveDepartmentInfo(TbDepartmentInfo department) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(department);
return department;
}
}
Junit Test
====>
package test;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import com.sunray.cpv.db.vo.TbDepartmentInfo;
//@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml" })
public class TTest extends AbstractTransactionalJUnit4SpringContextTests {
@Resource(name="TbDepartmentInfoDAO")
private TbDepartmentInfoDAO hello;
@Before
public void xx(){
System.out.println("sdfsdfsd");
}
@Test
public void main() {
// Resource res = new FileSystemResource("src/applicationContext.xml");
// BeanFactory factory = new XmlBeanFactory(res);
//
// IDepartmentInfoManage hello = (DepartmentInfoManageProxy) factory.getBean("DepartmentInfoManageProxy");
System.out.println("sdfgsdfsd11111");
TbDepartmentInfo department = new TbDepartmentInfo();
department.setVcdepartmentcode("1231231");
department.setVcdepartmentname("1231231");
department.setVcdepartmentabbreviation("1231231");
department.setBdeleteflag("1");
department.setNumdepartmenttype(1);
department.setNumsort(new BigDecimal(1));
department.setIparentid(new Long(1000000));
hello.saveDepartmentInfo(department);
System.out.println("sdfgsdfsd2222");
}
}