67,512
社区成员
发帖
与我相关
我的任务
分享
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)
<?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>
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;
}
}
package db.sample.dao.UmUserDAO.intf;
import db.sample.model.UmUser.UmUser;
/**
*
* @author Administrator
*/
public interface IUmUserDAO {
public UmUser getUserByName(String username);
}
/*
* 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.");
}
}