为什么我得到“createCriteria无效而没有活动事务”异常?

时间:2012-06-26 14:51:46

标签: java hibernate transactions junit

我正在使用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;
}

代码在“最终标准条件= session.createCriteria(Product.class);”时死亡除了......

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相同。

2 个答案:

答案 0 :(得分:3)

getCurrentSession()不会返回与openSession()相同的会话。前者返回上下文(默认情况下是线程绑定)会话。后者创建一个新会话,与上下文会话不同。

答案 1 :(得分:0)

从void testFindProductByTitle()中删除一行并粘贴Product findByTitle(String)方法,即:

final Transaction tx = session.beginTransaction();