使用Hibernate和Guice管理JavaSE中的事务

我正在编写一个使用GWT,Hibernate和Google

Guice(带有GIN)的相当简单的应用程序。我想做的是使用外部管理器(例如@Transactional在Spring中使用)来管理事务

,而不是使用EntityManager#getTransaction。我尝试使用@Transactional,但似乎不适用于我。

我已经使用注入了EntityManager Providers,如下所示:

/* import stuff */

public class DbProvider implements Provider<EntityManager> {

public EntityManager get() {

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persdb");

return emf.createEntityManager();

}

}

手动管理事务时,它似乎可以正常工作。我希望自动管理事务,也希望使用DBUnit进行自动化测试。

有人知道如何解决吗?

回答:

@Transactional在吉斯工作需要三件事情:

  • 您需要guice-persist.jar在类路径中
  • @Transactional调用方法的对象必须由Guice创建
  • 方法一定不能 private

以上是 使用Hibernate和Guice管理JavaSE中的事务 的全部内容, 来源链接: utcz.com/qa/398471.html

回到顶部