本文介绍了用Hibernate和Guice管理JavaSE中的事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用GWT,Hibernate和Google Guice(带GIN)的相当简单的应用程序。我想要做的是使用外部管理器来管理事务(比如在Spring中使用 @Transactional ),而不是使用 EntityManager#getTransaction 。我尝试使用 @Transactional ,但它似乎不适用于我。



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

  / * import stuff * / 

public class DbProvider实现Provider< EntityManager> {

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

}

它似乎在管理手动交易。我希望自动管理事务,也可以使用DBUnit进行自动化测试。



有谁知道如何解决这个问题?

@Transactional 在Guice工作:
$ b

  • 您需要 guice-persist.jar 在你的类路径中

  • @Transactional 方法的对象必须由Guice创建

  • 这些方法不能是 private


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?

解决方案

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

这篇关于用Hibernate和Guice管理JavaSE中的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 02:28