简体   繁体   English

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

[英]Managing transactions in JavaSE with Hibernate and Guice

I'm writing a rather simple application that uses GWT, Hibernate and Google Guice (with GIN). 我正在编写一个使用GWT,Hibernate和Google Guice(带有GIN)的相当简单的应用程序。 What I wanted to do is to have transactions managed using external manager (like using @Transactional in Spring), instead of using EntityManager#getTransaction . 我想要做的是使用外部管理器(例如,在Spring中使用@Transactional )来管理事务,而不是使用EntityManager#getTransaction I tried using @Transactional , but it doesn't seem to work for me. 我尝试使用@Transactional ,但是它似乎对我不起作用。

I have EntityManager already injected using Providers , like this: 我已经使用Providers注入了EntityManager,如下所示:

/* import stuff */

public class DbProvider implements Provider<EntityManager> {

    public EntityManager get() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persdb");
        return emf.createEntityManager();
    }

}

It seems to work properly when managing the transactions manually. 手动管理事务时,它似乎可以正常工作。 I wanted to have transactions managed automatically, also for making the automated test with DBUnit. 我希望自动管理事务,也希望使用DBUnit进行自动化测试。

Does anyone know how to solve that? 有人知道如何解决吗?

Having @Transactional work in Guice requires three things : 在Guice中进行@Transactional工作需要三件事

  • You need guice-persist.jar in your classpath 您需要在类路径中使用guice-persist.jar
  • The object on which the @Transactional methods are called must be created by Guice 调用@Transactional方法的对象必须由Guice创建
  • The methods must not be private 方法不能是private

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM