My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GuicePersistMultiModules  
Multiple persistence providers with Guice Persist
Updated Oct 16, 2011 by sberlin

Multiple Modules

So far we've seen how to use Guice Persist and JPA with a single persistence unit. This typically maps to one data store. Guice Persist supports using multiple persistence modules using the private modules API.

This means that you must be careful to install all JPA modules only in individual private modules:

Module one = new PrivateModule() {
  protected void configure() {
    install(new JpaModule("persistenceUnitOne"));
    // other services...
  }
};

Module two = new PrivateModule() {
  protected void configure() {
    install(new JpaModule("persistenceUnitTwo"));
    // other services...
  }
};

Guice.createInjector(one, two);

The injector now contains two parallel sets of bindings which are isolated from each other. When you bind services in module one those services will get the EntityManager from persistenceUnitOne and transactions around its database.

Similarly services in module two will have their own transactions, EntityManagers etc., and these should typically not cross.

Comment by pas...@gmail.com, Mar 8, 2011

What about if you require multiple persistence units in a single module? I.e. I have one database for configuration data, another database for logs. Whenever I change some configuration data, I need to also add a log to the other database.

Comment by MariaKog...@gmail.com, May 25, 2011

The same story. Why can't I just use @EntityManager?("myPersistenceUnit1") in one place and @EntityManager?("anotherPersistenceUnit2") in another ?

Comment by joshnet2030@gmail.com, Sep 6, 2011

How do you inject each persistence module ? is there something like @Inject("myPersistenceUnit1") EntityManager? em ;

Comment by noctariushtc@gmail.com, Sep 6, 2011

@Inject @Named("myPersistenceUnit1") EntityManager? em;

Comment by michael....@gmail.com, Mar 14, 2012

After a some headaches imho due to sparse documentation on that topic I found an explanation how to setup PersistFilter? in a ServletModule with multiple persist modules:

https://groups.google.com/forum/#!topic/google-guice/2VK-bdsnjZc/discussion

Furthermore I want to point at the following post, as it helped me to understand that persisting objects to the database requires the Session or EntityManager? not being exposed but kept in private space:

https://groups.google.com/forum/?fromgroups#!searchin/google-guice/@Transactional$20guice-persist$20not/google-guice/kbXr4k0N2QE/W_6VAvo5Mn0J


Sign in to add a comment
Powered by Google Project Hosting