我正在使用Hibernate 4.0.1.Final编写一个独立的Java应用程序。在测试我的数据访问层时,我收到以下异常
org.hibernate.HibernateException:如果没有,createCriteria无效 活跃交易
搜索对象时。我正在尝试将我的事务级代码与我的数据库操作代码分开。我的JUnit测试是
@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
单元测试调用的代码是......
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;
}
在行的DAO中抛出异常
final Criteria crit = session.createCriteria(State.class).add(Restrictions.eq("abbrev", abbrev));
如何调整我的JUnit测试(或我的DAO代码)以消除此异常并使我的代码正常运行?这是我的hibernate.cfg.xml
文件......
<?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>
答案 0 :(得分:9)
你可以移动
session = sessionFactory.openSession();
tx = session.beginTransaction();
在您的DAO方法中或将新会话绑定到@Before
方法中的当前线程。
您已指定hibernate应使用ThreadLocalSessionContext
。但是,没有任何东西可以将Session绑定到当前线程。
ThreadLocalSessionContext
提供了一种静态bind(org.hibernate.Session session)
方法,您可以使用该方法确保sessionFactory.getCurrentSession()
可以访问您已打开的会话。
只是打开一个新会话不会将其绑定到该线程。