0

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?

mhaligowski
  • 2,182
  • 20
  • 26

1 Answers1

6

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
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • thanks a lot! if only that worked:) where do you start the PersistService? – mhaligowski Mar 13 '11 at 11:37
  • @halish: You can start a `PersistService` wherever you want (when your application starts, say). In a web application, it can be done with `PersistFilter`, which starts the service when it's created and stops it when destroyed. In your own application, you need to start it before you do anything that uses it. – ColinD Mar 13 '11 at 17:33