本文介绍了Spring / Hibernate / JUnit - 没有Hibernate Session绑定到Thread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在测试用例中访问当前的hibernate会话,并得到以下错误:

我明显错过了某种设置,但不知道是什么。



任何帮助将不胜感激。这是我在Hibernate / Spring等的第一次破解,学习曲线肯定是陡峭的!



代码如下:

犯规类:

  public class DbUnitUtil extends BaseDALTest {
@Test
public void exportDtd( )抛出异常
{

Session session = sessionFactory.getCurrentSession();

session.beginTransaction();
连接hsqldbConnection = session.connection();

IDatabaseConnection connection = new DatabaseConnection(hsqldbConnection);

//写入DTD文件
FlatDtdDataSet.write(connection.createDataSet(),new FileOutputStream(test.dtd));


$ / code $ / pre
$ b $基本类别

  @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {classpath:applicationContext.xml})
public class BaseDALTest extends AbstractJUnit4SpringContextTests {
public BaseDALTest()
{
super();
}
@Resource
protected SessionFactory sessionFactory;
}

applicationContext.xml:

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">

< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassName>
< value> org.hsqldb.jdbcDriver< / value>
< / property>
< property name =url>
< value> jdbc:hsqldb:mem:sample< / value>
< / property>
< property name =username>
<值> sa< /值>
< / property>
< property name =password>
<值>< /值>
< / property>
< / bean>

< bean id =sessionFactoryclass =com.foo.spring.AutoAnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =entityPackages>
< list>
<值> com.sample.model< /值>
< / list>
< / property>
< property name =schemaUpdate>
<值> true< /值>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.HSQLDialect
< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>
< / beans>


解决方案

错误,代码。

首先,在您的上下文中添加一个事务管理bean:

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

第二件事,扩展AbstractTransactionalJUnit4SpringContextTests

  public class BaseDALTest 
extends AbstractTransactionalJUnit4SpringContextTests {

第三件事,用

  @TransactionConfiguration 
@Transactional

如果您的交易分界是正确的(围绕您的dao或服务),您应该完成。



将代码中的会话和事务处理代码都放在代码中(甚至是在你的测试中)是不好的。

I'm trying to access the current hibernate session in a test case, and getting the following error:

I've clearly missed some sort of setup, but not sure what.

Any help would be greatly appreciated. This is my first crack at Hibernate / Spring etc, and the learning curve is certainly steep!

Code follows:

The offending class:

public class DbUnitUtil extends BaseDALTest {
    @Test
    public void exportDtd() throws Exception
    {

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();
        Connection hsqldbConnection = session.connection();

        IDatabaseConnection connection = new DatabaseConnection(hsqldbConnection);

        // write DTD file
        FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream("test.dtd"));
    }
}

Base class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseDALTest extends AbstractJUnit4SpringContextTests {
    public BaseDALTest()
    {
        super();
    }
        @Resource
        protected SessionFactory sessionFactory;
}

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>org.hsqldb.jdbcDriver</value>
        </property>
        <property name="url">
            <value>jdbc:hsqldb:mem:sample</value>
        </property>
        <property name="username">
            <value>sa</value>
        </property>
        <property name="password">
            <value></value>
        </property>
    </bean>

    <bean id="sessionFactory" class="com.foo.spring.AutoAnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="entityPackages">
            <list>
                <value>com.sample.model</value>
            </list>
        </property>
        <property name="schemaUpdate">
            <value>true</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
</beans>
解决方案

Wrong, that will just fill your code with session management code.

First, add a transaction management bean in your context:

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

The second thing, extend AbstractTransactionalJUnit4SpringContextTests

    public class BaseDALTest
           extends AbstractTransactionalJUnit4SpringContextTests{

Third thing, annotate you test class with

    @TransactionConfiguration
    @Transactional

If your transaction demarcation is correct(surrounding your dao or service) you should be done.

It's not nice to sprinkle session and transaction handling code all around your code (even inside your tests).

这篇关于Spring / Hibernate / JUnit - 没有Hibernate Session绑定到Thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:59