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

fnever 2009-05-04 08:34:38
不知道是不是hibernate3.2强制要求进行事务管理的,网上很多教程都没有提到
我就是想测试一下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 的配置如下
<?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/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="umUserDAO" class="db.sample.dao.UmUserDAO.UmUserDAO" >
<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>





</beans>

entity bean文件是netbean自动生成的,应该没什么问题,我只不过让他继承了一个我指定的 BasePO类,这个类是implements java.io.Serializable的
BaseDAO 如下,很简单(没用到..)
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;
}
}


interface
package db.sample.dao.UmUserDAO.intf;
import db.sample.model.UmUser.UmUser;
/**
*
* @author Administrator
*/
public interface IUmUserDAO {
public UmUser getUserByName(String username);
}


这个interface 的实现:
/*
* 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;
}
}


最后的测试类
/*
* 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.");
}

}


其实我感觉应该是spring.xml 有问题,但实在看不出来在什么地方,
顺便说一下,我用的是glassfish2.0,应该没什么影响吧?...




还有一个小问题,spring 应该是可以往一个pojo的爸爸里注射,然后让这个儿子调用爸爸的被注射的属性的吧?
...全文
224 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fnever 2009-05-05
  • 打赏
  • 举报
回复
兄弟们,我冤枉啊,我用了,我在spring的xml里配了的
<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>

不过我现在知道原因了,是aop的表达式错了,谢谢大家的关注
老紫竹 2009-05-05
  • 打赏
  • 举报
回复
恩,看错误也是
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

你的线程要求使用事务,因为你的配置里不允许创建非事务的线程!

我想问一句,你为何不用事务呢?
dxpws 2009-05-05
  • 打赏
  • 举报
回复
up
ben0759 2009-05-05
  • 打赏
  • 举报
回复
<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>

楼主使用了spring的hibernate,为什么我看你的dao却没有注入hibernateSupport。
你试试看取消事务呢。
fnever 2009-05-04
  • 打赏
  • 举报
回复
困饶了很久了..
ybgiser 2009-05-04
  • 打赏
  • 举报
回复
路过,帮顶

67,512

社区成员

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

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