我正在使用 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
文件……</p>
<?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>