org.hibernate.HibernateException: No Session found for current thread

I’m creating a little project at home to record some data and I’m using Spring & Hibernate. This problem took me a couple of hours to work out, so I thought I’d post my solution as it wasn’t listed in the 20 or so pages I visited to fix the problem.

So, you’ve just set up a DAO and wired the session factory, data source and transaction manager up, but your junit test is still reporting:

org.hibernate.HibernateException: No Session found for current thread

jdbc.properties

jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver
jdbc.url=jdbc:hsqldb:mem:testdb
jdbc.username=sa
jdbc.password=

application context

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

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="packagesToScan" value="uk.co.vsf.repository.domain" />
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<context:property-placeholder location="jdbc.properties" />

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

	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />


	<bean id="baseDao" class="uk.co.vsf.repository.dao.impl.BaseDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="readingDao"
		  class="uk.co.vsf.repository.dao.impl.ReadingDaoImpl"
		  parent="baseDao">
	</bean>

	<context:component-scan base-package="uk.co.vsf" />
</beans>

unit test

@ContextConfiguration(locations = { "classpath:aCtx-base.xml", "classpath:aCtx-repository.xml", "classpath:aCtx-dao.xml" })
@TransactionConfiguration(defaultRollback = true)
public class ReadingDaoTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private ReadingDao dao;

    @Autowired
    private SessionFactory sessionFactory;

    @Test
    public void readingsCount() {
        assertEquals(0, dao.totalReadings());
    }

    @Test
    public void saveReading() {

        assertEquals(0, dao.totalReadings());

        Reading reading = new ReadingImpl(BigInteger.valueOf(123));
        dao.addReading(reading);

        assertEquals(1, dao.totalReadings());
    }
}

What can possibly be wrong? Well as I say, I read solution after solution and still nothing fixed my problem, until I took another look at my unit test and realised I should be extending AbstractTransactionalJUnit4SpringContextTests!

Problem sorted – hope this helps someone else