Thanks again but I still have problems. Before getting this to work in a 
servlet (GWT-P dispatch handler), I am trying to run it inside a simple 
main(). I'm getting the following error when running the code below. It's 
compaining when invoking the ApplicationInitializer. 

Exception in thread "main" com.google.inject.ConfigurationException: Guice 
configuration errors:

1) Unable to create binding for com.google.inject.persist.PersistService. 
It was already configured on one or more child injectors or private modules
    bound at 
com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:69)
  If it was in a PrivateModule, did you forget to expose the binding?
  while locating com.google.inject.persist.PersistService
    for parameter 0 at SomeService // this line: @Inject 
ApplicationInitializer(PersistService service) {
  while locating SomeService$ApplicationInitializer

1 error
at 
com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
at 
com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:961)
at 
com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at SomeService // this line: 
injector.getInstance(ApplicationInitializer.class);
...

public class SomeService {
    @Inject @MyDataSourceOne EntityManager em;
    // ... 

    public static void main(String[] args) {
        Module one = new PrivateModule() {
            protected void configure() {
                final JpaPersistModule jpm = new 
JpaPersistModule("pu-name");
                jpm.properties(new Properties());
                install(jpm);
                final Provider<EntityManager> entityManagerProvider = 
                        binder().getProvider(EntityManager.class); 
                
bind(EntityManager.class).annotatedWith(MyDataSourceOne.class).toProvider(entityManagerProvider);
                
expose(EntityManager.class).annotatedWith(MyDataSourceOne.class);
            }
        };
        // similar code for "two"

        Injector injector = Guice.createInjector(one, two);
        injector.getInstance(ApplicationInitializer.class);

        SomeService service = injector.getInstance(SomeService.class);

        // run one of this service's methods
    }

    static class ApplicationInitializer {
        @Inject ApplicationInitializer(PersistService service) {
            service.start();
        }
    }
}

If I do *not *use ApplicationInitilizer, I'm getting a NullPointerException:

Exception in thread "main" com.google.inject.ProvisionException: Guice 
provision errors:

1) Error in custom provider, java.lang.NullPointerException
  while locating com.google.inject.persist.jpa.JpaPersistService
  while locating javax.persistence.EntityManager
  at SomeService // this line: 
bind(EntityManager.class).annotatedWith(MyDataSourceOne.class).toProvider(entityManagerProvider);
  while locating javax.persistence.EntityManager annotated with 
@....MyDataSourceOne()
    for field at SomeService...
  while locating SomeService

1 error
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
at 
com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at SomeService // this line: SomeService service = 
injector.getInstance(SomeService.class);
Caused by: java.lang.NullPointerException
at 
com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:70)



On Monday, January 2, 2012 2:29:54 AM UTC+1, scl wrote:
>
> If you want to expose the EntityManager you can do this as follows: 
>
> // in your private module: 
> final Provider<EntityManager> entityManagerProvider = 
> binder().getProvider(EntityManager.class); 
> bind(EntityManager.class).annotatedWith(MyDataSourceOne.class).toProvider(entityManagerProvider);
>  
>
> expose(EntityManager.class).annotatedWith(MyDataSourceOne.class); 
>
> Now the EntityManager is available outsied the private module: 
>
> @Inject @MyDataSourceOne 
> EntityManager entityManager; 
>
>
> And here is the code to the session provider you asked for: 
>
> public class SessionProvider implements Provider<Session> { 
>     /** The entity manger to retrieve the session from. */ 
>     @Inject 
>     private Provider<EntityManager> entityManagerProvider; 
>
>     /** 
>      * @return the Hibernate session, being the delegate of the entity 
> manager provided by the injected entity manager provider. 
>      */ 
>     @Override 
>     public Session get() { 
>         final Session session = (Session) 
> entityManagerProvider.get().getDelegate(); 
>         // configure session i.e. flush mode or filtering 
>         return session; 
>     } 
>
>
On Monday, January 2, 2012 2:29:54 AM UTC+1, scl wrote:
>
> If you want to expose the EntityManager you can do this as follows: 
>
> // in your private module: 
> final Provider<EntityManager> entityManagerProvider = 
> binder().getProvider(EntityManager.class); 
> bind(EntityManager.class).annotatedWith(MyDataSourceOne.class).toProvider(entityManagerProvider);
>  
>
> expose(EntityManager.class).annotatedWith(MyDataSourceOne.class); 
>
> Now the EntityManager is available outsied the private module: 
>
> @Inject @MyDataSourceOne 
> EntityManager entityManager; 
>
>
> And here is the code to the session provider you asked for: 
>
> public class SessionProvider implements Provider<Session> { 
>     /** The entity manger to retrieve the session from. */ 
>     @Inject 
>     private Provider<EntityManager> entityManagerProvider; 
>
>     /** 
>      * @return the Hibernate session, being the delegate of the entity 
> manager provided by the injected entity manager provider. 
>      */ 
>     @Override 
>     public Session get() { 
>         final Session session = (Session) 
> entityManagerProvider.get().getDelegate(); 
>         // configure session i.e. flush mode or filtering 
>         return session; 
>     } 
>
>
On Monday, January 2, 2012 2:29:54 AM UTC+1, scl wrote:
>
> If you want to expose the EntityManager you can do this as follows: 
>
> // in your private module: 
> final Provider<EntityManager> entityManagerProvider = 
> binder().getProvider(EntityManager.class); 
> bind(EntityManager.class).annotatedWith(MyDataSourceOne.class).toProvider(entityManagerProvider);
>  
>
> expose(EntityManager.class).annotatedWith(MyDataSourceOne.class); 
>
> Now the EntityManager is available outsied the private module: 
>
> @Inject @MyDataSourceOne 
> EntityManager entityManager; 
>
>
> And here is the code to the session provider you asked for: 
>
> public class SessionProvider implements Provider<Session> { 
>     /** The entity manger to retrieve the session from. */ 
>     @Inject 
>     private Provider<EntityManager> entityManagerProvider; 
>
>     /** 
>      * @return the Hibernate session, being the delegate of the entity 
> manager provided by the injected entity manager provider. 
>      */ 
>     @Override 
>     public Session get() { 
>         final Session session = (Session) 
> entityManagerProvider.get().getDelegate(); 
>         // configure session i.e. flush mode or filtering 
>         return session; 
>     } 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-guice/-/hoFsdng5XIgJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to