0
我試圖通過EntityManager與Guice注入一起使用jpa持久性插入實體對象。 它工作正常,當我試圖插入一個對象使用我的主要類的oject條目反映在我的數據庫。 但是,當我執行我的Junit測試案例時,它顯示我錯誤:「java.lang.IllegalStateException:Transaction not active」,while db in db Remaining methods proplerly works。休眠JPA事務運行TestCases時沒有激活錯誤,但在主類中執行時工作正常
============== AbstractHibernateRepository<T> Class==================
@Inject
private EntityManager em;
public AbstractHibernateRepository(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected EntityManager getEntityManager() {
return this.em;
}
@Transactional
public T create(T entity) {
EntityManager entityManager = getEntityManager();
if (!entityManager.getTransaction().isActive())
entityManager.getTransaction().begin();
try {
entityManager.persist(entity);
if (entityManager.getTransaction().isActive()){
entityManager.getTransaction().commit();
}
else
entityManager.flush();
} catch (Exception e) {
System.out.println("======>>>>>");
if (entityManager.getTransaction().isActive())
entityManager.getTransaction().rollback();
e.printStackTrace();
}
return entity;
}
================================
================== AvailableServerRepository.class ==============
public class AvailableServerRepository extends
AbstractHibernateRepository<AvailableServerEntity> implements
IAvailableServerRepository {
@Override
public AvailableServerEntity insertAvailableServer(
AvailableServerEntity availableServer) {
return create(availableServer);
}
}
====================================
public class AvailableServerRepositoryTest {
private static Injector injector = null;
private static PersistenceInitializer initializer = null;
@BeforeClass
public static void environmentSetup() {
injector = Guice.createInjector(new MainModule(), new JpaPersistModule(
"proplet-indexer"));
initializer = injector.getInstance(PersistenceInitializer.class);
initializer.startServer();
}
@AfterClass
public static void environmentClean() {
if(initializer != null)
initializer.stopServer();
}
/**
* Test of insertAvailableServer method, of class AvailableServerRepository.
*/
@Test
public void testInsertAvailableServer() {
AvailableServerRepository availableServerRepository = injector
.getInstance(AvailableServerRepository.class);
AvailableServerEntity availableServer = createAvailableServer();
assertNotNull(availableServerRepository.insertAvailableServer(availableServer));
}
====================================
Error while it throws while running testCases is
java.lang.IllegalStateException: Transaction not active
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:70)
at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInterceptor.java:87)
at com.exzeo.indexer.persistence.repository.impl.AvailableServerRepository.insertAvailableServer(AvailableServerRepository.java:34)
at com.exzeo.indexer.persistence.repository.impl.AvailableServerRepositoryTest.testInsertAvailableServer(AvailableServerRepositoryTest.java:56)
發佈完整堆棧跟蹤 – Chaitanya 2014-09-30 13:32:55
嘿柴塔尼亞它只顯示此消息,而執行測試用例 – 2014-09-30 15:32:13