I’m using Hibernate 4.0.1.Final to write a standalone Java app. When testing my data access layer, I’m getting the following exception
org.hibernate.HibernateException: createCriteria is not valid without
active transaction
when searching for an object. I’m trying to separate my transaction level code from my database operational code. My JUnit test is
@Before
public void setUpDAOTest() {
final Configuration configuration = new Configuration();
configuration.configure().setProperty("hibernate.show_sql", "false");
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
orgDao = new OrganizationDAOImpl(sessionFactory);
session = sessionFactory.openSession();
tx = session.beginTransaction();
} // setUp
@Test
public void testFindStateByAbbrev() {
final String abbrev = testProps.getProperty("test.state.abbrev");
final State state = orgDao.findStateByAbbrev(abbrev);
Assert.assertNotNull(state);
Assert.assertEquals(testProps.getProperty("test.state.abbrev"), state.getAbbrev());
} // testFindStateByAbbrev
and the code the unit test invokes is …
public class OrganizationDAOImpl extends AbstractDAO implements OrganizationDAO {
…
public State findStateByAbbrev(final String abbrev) {
State ret = null;
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(State.class).add(Restrictions.eq("abbrev", abbrev));
final List<State> results = crit.list();
if (results != null && results.size() > 0) {
ret = results.get(0);
} // if
return ret;
}
The exception is thrown in the DAO at the line,
final Criteria crit = session.createCriteria(State.class).add(Restrictions.eq("abbrev", abbrev));
How can I adjust my JUnit test (or my DAO code) to eliminate this exception and make my code run properly? Here’s my hibernate.cfg.xml file …
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbid</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping class="org.myco.myproject.orgsclient.model.Organization" />
<mapping class="org.myco.myproject.orgsclient.model.State" />
</session-factory>
</hibernate-configuration>
You can either move
inside your DAO method or bind the new session to the current thread in the
@Beforemethod.You have specified that hibernate should use the
ThreadLocalSessionContext. However, there is nothing to bind a Session to the current thread.ThreadLocalSessionContextprovides a staticbind(org.hibernate.Session session)method which you could use it ensure thatsessionFactory.getCurrentSession()can access the session which you have opened.Just opening a new session doesn’t bind it to the thread.