0

Hibernate 4.0.1.Final を使用しています。DAO オブジェクトで JUnit テストを実行しようとしていますが、「createCriteria is not valid without active transaction」という例外が発生します。奇妙なことに、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);」で終了します。ただし…</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 と同じです。

4

2 に答える 2

3

getCurrentSession()と同じセッションを返しませんopenSession()。前者は、コンテキストに応じた (デフォルトではスレッドにバインドされた) セッションを返します。後者は、コンテキスト セッションとは異なる新しいセッションを作成します。

于 2012-06-26T14:54:59.817 に答える
0

void testFindProductByTitle() から 1 行を切り取って、Product findByTitle(String) メソッドに貼り付けるだけです。

final Transaction tx = session.beginTransaction();
于 2015-06-25T09:49:01.790 に答える