spring2.5 + hibernate3.2 annotation 配置事务时出错!不知道是不是配置文件错了,大家帮帮忙啊!

Mcikeay 2008-04-09 09:09:05
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");
}

}

...全文
639 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sonald 2009-05-14
  • 打赏
  • 举报
回复
我也遇到同样的问题 还请明白的师兄指点 谢谢

去掉<prop key="hibernate.current_session_context_class">thread </prop> 就正常了 但 显然不是我们想要的


best regards
xiang fang
Mcikeay 2008-04-09
  • 打赏
  • 举报
回复
在调试的时候出现了以下异常:
org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
at $Proxy24.saveOrUpdate(Unknown Source)
at com.sunray.cpv.db.dao.TbDepartmentInfoDAO.saveDepartmentInfo(TbDepartmentInfoDAO.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy17.saveDepartmentInfo(Unknown Source)
at com.sunray.cpv.services.security.impl.DepartmentInfoManageImpl.saveDepartmentInfo(DepartmentInfoManageImpl.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy21.saveDepartmentInfo(Unknown Source)
at com.sunray.cpv.services.security.proxy.DepartmentInfoManageProxy.saveDepartmentInfo(DepartmentInfoManageProxy.java:154)
at test.TTest.main(TTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:198)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:274)
at org.springframework.test.context.junit4.SpringMethodRoadie$2.run(SpringMethodRoadie.java:207)
at org.springframework.test.context.junit4.SpringMethodRoadie.runBeforesThenTestThenAfters(SpringMethodRoadie.java:254)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:234)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:204)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:146)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:151)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

个人认为是:在sessionFactory 获取的session时 没有绑定到当前的事务,或者是事务没有开启!

第一次使用annotation 注解

因改是配置错了,但不知道是哪里错了,大家来看看,瞧瞧,出点主意啊!

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧