Managing transactions in JavaSE with Hibernate and Guice
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.createEntityMa开发者_运维百科nagerFactory("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?
Having @Transactional work in Guice requires three things:
- You need
guice-persist.jarin your classpath - The object on which the
@Transactionalmethods are called must be created by Guice - The methods must not be
private
加载中,请稍侯......
精彩评论