我正在使用 Hibernate 4.0.1.Final。我正在尝试对我的 DAO 对象运行 JUnit 测试,但遇到了异常,“createCriteria 在没有活动事务的情况下无效”。奇怪的是,我在调用 DAO 方法之前开始了事务。我有
@Test
public void testFindProductByTitle() {
final Session session = sessionFactory.openSession();
final Transaction tx = session.beginTransaction();
final Product product = productDao.findByTitle(testProps.getProperty("test.product.name"));
tx.commit();
session.close();
Assert.assertNotNull(product);
Assert.assertEquals(testProps.getProperty("test.product.name"), product.getName());
}
我的 DAO 包括
public Product findByTitle(String title) {
final Session session = sessionFactory.getCurrentSession();
final Criteria criteria = session.createCriteria(Product.class);
criteria.add(Restrictions.eq("name", title));
final List<Product> results = criteria.list();
final Product result = results.get(0);
return result;
}
代码在“final Criteria criteria = session.createCriteria(Product.class);”处终止 除了……</p>
org.hibernate.HibernateException: createCriteria is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:346)
at $Proxy21.createCriteria(Unknown Source)
at org.myco.myproj.dido.dao.lycea.ProductDAOImpl.findByTitle(ProductDAOImpl.java:20)
at org.myco.myproj.dao.ProductDAOTest.testFindByProductTitle(ProductDAOTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
任何想法出了什么问题?Junit 测试中的 sessionFactory 与我初始化 DAO 测试时使用的 sessionFactory 相同。