2011-03-13 26 views
0

我正在編寫一個使用GWT,Hibernate和Google Guice(使用GIN)的相當簡單的應用程序。我想要做的是使用外部管理器進行事務管理(例如在Spring中使用@Transactional),而不是使用EntityManager#getTransaction。我嘗試使用@Transactional,但它似乎不適用於我。使用Hibernate和Guice管理JavaSE中的事務

我的EntityManager已經使用Providers,這樣的注射:

/* import stuff */ 

public class DbProvider implements Provider<EntityManager> { 

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

} 

看來手動管理的事務時才能正常工作。我希望自動管理事務,也可以使用DBUnit進行自動化測試。

有誰知道如何解決?

回答

5

@Transactional工作吉斯requires three things

  • 您需要guice-persist.jar在類路徑
  • 上被稱爲@Transactional方法必須由吉斯創建對象
  • 的方法一定不能private
+0

非常感謝!如果只是工作:) 你在哪裏啓動PersistService? – mhaligowski 2011-03-13 11:37:03

+0

@halish:你可以在任何你想要的地方啓動一個'PersistService'(當你的應用程序啓動時,說)。在Web應用程序中,可以使用PersistFilter來完成,它在創建時啓動服務,並在銷燬時停止服務。在你自己的應用程序中,你需要在你做任何使用它的東西之前啓動它。 – ColinD 2011-03-13 17:33:04

相關問題