weixin_42309440 2009-05-04 07:53
浏览 252
已采纳

很简单的spring+hibernate问题,但是就是运行不了,应该是关于transaction 的

我就是想测试一下spring+hibernate,网上有的教程提到了spring+hibernate 时的事务处理,有的提都没提,貌似也能通过,不知道是不是版本问题...但我明明加了事务处理,运行的时候还是报错:
Testcase: testGetUserByName(db.sample.dao.UmUserDAO.UmUserDAOTest): Caused an ERROR
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
at db.sample.dao.UmUserDAO.UmUserDAO.getUserByName(UmUserDAO.java:34)
at db.sample.dao.UmUserDAO.UmUserDAOTest.testGetUserByName(UmUserDAOTest.java:50)

我用的是netbean 6.5,spring 2.5,hibernate 3.2,mysql

spring 的配置如下
[code="java"]<?xml version="1.0" encoding="UTF-8"?>
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/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://67.210.110.200/easycat0_cardshop</value>
    </property>
    <property name="username">
        <value>easycat0_cs</value>
    </property>
    <property name="password">
        <value>123456</value>
    </property>
</bean>

<bean id="HibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.c3p0.minPoolSize">5</prop>
            <prop key="hibernate.c3p0.maxPoolSize">200</prop>
            <prop key="hibernate.c3p0.timeout">600</prop>
            <prop key="hibernate.c3p0.max_statement">50</prop>
        </props>
    </property>
</bean>



<!-- Hibernate SessionFactory -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref local="DataSource"/>
    </property>
    <property name="hibernateProperties">
        <ref bean="HibernateProperties" />
    </property>
    <!-- Must references all OR mapping files. -->
    <property name="mappingResources">
        <list>
            <value>db/sample/model/UmUser/UmUser.hbm.xml</value>
        </list>
    </property>


</bean>

<!-- DAO 定义
<bean id="DAOTemplate" abstract="true" lazy-init="true" class="db.base.BaseDAO">
    <property name="sessionFactory" ref="SessionFactory"/>
</bean>

-->


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


<aop:config>
    <aop:pointcut id="serviceMethods" expression="execution(* db.sample.dao.intf.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

[/code]

entity bean文件是netbean自动生成的,应该没什么问题,我只不过让他继承了一个我指定的 BasePO类,这个类是implements java.io.Serializable的

BaseDAO 如下,很简单[code="java"]
import org.hibernate.SessionFactory;
public abstract class BaseDAO
implements IBaseDAO {

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

}
[/code]

具体实现的interface:
[code="java"]
package db.sample.dao.UmUserDAO.intf;
import db.sample.model.UmUser.UmUser;
/**
*

  • @author Administrator */ public interface IUmUserDAO { public UmUser getUserByName(String username); } [/code]

这个interface 的实现:
[code="java"]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor. */

package db.sample.dao.UmUserDAO;
import db.sample.dao.UmUserDAO.intf.IUmUserDAO;
import db.sample.model.UmUser.UmUser;
import db.base.BaseDAO;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.*;
/**
*

  • @author Administrator */

@Transactional(readOnly = true)
public class UmUserDAO
implements IUmUserDAO{
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public UmUser getUserByName(String username)
{
    UmUser user=(UmUser)this.getSessionFactory().getCurrentSession().createQuery("from um_user user where user.username=\"?\"").setParameter(0, username).list().get(0);
    return user;
}

}
[/code]

最后是测试类
[code="java"]
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor. */

package db.sample.dao.UmUserDAO;

import db.sample.model.UmUser.UmUser;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
*

  • @author Administrator
    */
    public class UmUserDAOTest {

    public UmUserDAOTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**

    • Test of getUserByName method, of class UmUserDAO. */ @Test public void testGetUserByName() { System.out.println("getUserByName"); String username = "guest"; FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext(new String[]{"src/META-INF/Spring.xml"}); UmUserDAO instance = (UmUserDAO)appContext.getBean("umUserDAO"); UmUser result = instance.getUserByName(username); //assertEquals(expResult, result); // TODO review the generated test code and remove the default call to fail. // fail("The test case is a prototype."); }

}
[/code]

其实我感觉应该是spring.xml 有问题,但实在看不出来在什么地方,
顺便说一下,我用的是glassfish2.0,应该没什么影响吧?...
[b]问题补充:[/b]
存在的啊
db.sample.dao.UmUserDAO.intf.IUmUserDAO
就是那个interface..

  • 写回答

1条回答 默认 最新

  • 邓臻昊 2009-05-04 08:32
    关注

    似乎是你这一句

    中的db.sample.dao.intf.*.*(..)这个路径不存在吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 绘制变量u1,u2与gg函数值的图,当u3=600时与u3=500时两个图像如何在一个三维图中表示
  • ¥20 码源版uec++编译环境问题
  • ¥15 codesys在仿真一次后无法增删变量
  • ¥20 RC522开发硬件系统刷卡异常
  • ¥15 simpack与simulink联合仿真
  • ¥15 VBA显示应用程序定义或者对象定义错误:
  • ¥15 关于#linux#的问题:minio输入密码能登录,但登录后为空白页面
  • ¥200 数据库架构方案有偿咨询
  • ¥15 matlab对包含滑台的机械臂求正逆解与轨迹规划
  • ¥15 Android13 systemui悬浮通知加载流程