Managing transactions in JavaSE with Hibernate and Guice

时光毁灭记忆、已成空白 提交于 2019-12-09 13:58:27

问题


I'm writing a rather simple application that uses GWT, Hibernate and Google Guice (with GIN). What I wanted to do is to have transactions managed using external manager (like using @Transactional in Spring), instead of using EntityManager#getTransaction. I tried using @Transactional, but it doesn't seem to work for me.

I have EntityManager already injected using Providers, like this:

/* 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.

Does anyone know how to solve that?


回答1:


Having @Transactional work in Guice requires three things:

  • You need guice-persist.jar in your classpath
  • The object on which the @Transactional methods are called must be created by Guice
  • The methods must not be private


来源:https://stackoverflow.com/questions/5287985/managing-transactions-in-javase-with-hibernate-and-guice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!