zhouxiuliang 2010-10-18 12:16 采纳率: 0%
浏览 286
已采纳

关于spring事务管理的问题

最近刚学spring,在事务管理这自己写了个小例子,可是老运行不通过,请大虾看看。下面是我的代码:
首先是DAO接口,UserDaoService:
package org.ultimania.dao;

import java.util.List;
import org.ultimania.model.User;

public interface UserDaoService {
public List getAll();
}
接口实现,UserDaoImpl:
package org.ultimania.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.ultimania.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDaoService{
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.REQUIRED)
public List getAll(){
List list = this.getHibernateTemplate().find("from User");
return list;
}

}
接下来是我的测试代码:
package org.ultimania.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.ultimania.dao.UserDaoImpl;
import org.ultimania.dao.UserDaoService;
import org.ultimania.model.User;

public class UserDaoTest {

public static void main(String[] args){
    List<User> list = null;
    UserDaoService userDaoImpl ;
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    userDaoImpl = (UserDaoService)ctx.getBean("userDao");
    try{
        list = userDaoImpl.getAll();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(list==null){
            System.out.println("faled");
        }else{
            System.out.println(list.size());
        }
    }
}

}
然后是spring配置文件,applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
        value="classpath:hibernate.cfg.xml">
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

<bean id="userDao" class="org.ultimania.dao.UserDaoImpl">
    <property name="hibernateTemplate">
        <ref bean="hibernateTemplate" />
    </property>
</bean>


我用的是注解式事务管理的方式,hibernate的东西是没错的,现在的问题是如果我在UserDaoImpl不添加事务声明@Transactional(propagation=Propagation.REQUIRED),运行是没有错误的,可是我一添加这句声明,运行就错了。下面是异常信息:
Exception in thread "main"
java.lang.AbstractMethodError: org.springframework.orm.hibernate3.SpringTransactionFactory.beginTransaction(Lorg/hibernate/jdbc/JDBCContext;Lorg/hibernate/transaction/TransactionFactory$Context;)Lorg/hibernate/Transaction;
at org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:271)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1079)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:558)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy5.getAll(Unknown Source)
at org.ultimania.test.UserDaoTest.main(UserDaoTest.java:19)
这个问题困扰我好久了,一直没有解决,只能请大虾们帮我看看了。

  • 写回答

4条回答

  • oyj7454103 2010-10-18 13:07
    关注

    [quote]@Transactional(propagation=Propagation.REQUIRED),运行是没有错误的,可是我一添加这句声明,运行就错了[/quote]

    相应的jar 包 是否加入?
    annotation 的版本和hibernate 版本是否匹配?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波