@Inject EntityManager - Guice 3.0

4,578 views
Skip to first unread message

Gabriel Cardelli

unread,
Jun 15, 2011, 2:09:54 PM6/15/11
to google-guice
Hi,
i'm trying to configure a app using Wicket + Guice 3.0 + Guice-Persist
3.0

I Have a Module:
public class EventosModule extends AbstractModule {

public final static String persistenceUnit = "eventosImpa";

public void configure() {
install(new JpaPersistModule(persistenceUnit));
}

}

Store:

@Singleton
public class EventoStoreImpl implements EventoStore {

@Inject
private Provider<EntityManager> entityManager;

@Transactional
public void save(Evento object) {

entityManager.get().persist(object);

}
}

When i try to execute my app i got this error:

1) No implementation for javax.persistence.EntityManager was bound.
while locating
com.google.inject.Provider<javax.persistence.EntityManager>
for field at
br.impa.apps.eventos.store.impl.EventoStoreImpl.entityManager(EventoStoreImpl.java:
21)


Can anyone help me?

jMotta

unread,
Jun 15, 2011, 2:44:25 PM6/15/11
to google...@googlegroups.com
Gabriel,

I've never used Guice Persist, but when I've used any type of provider, including an entity manager provider, I had to bind it in a module, but once you're installing this JpaPersistModule this probably should work.

Apparently there is something related to initiate the persistence service while using this JpaPersistModule, check this out in this link.

Let us know if it helps you! :)

Jayr Motta
Software Developer





--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


alex opn

unread,
Jun 16, 2011, 3:35:08 AM6/16/11
to google...@googlegroups.com
Did you maybe forget to start it:

Finally, you must decide when the persistence service is to be started by invoking start() on PersistService. I typically use a simple initializer class that I can trigger at a time of my choosing:

public class MyInitializer { 
       
@Inject MyInitializer(PersistService service) {
                service
.start();
 
               
// At this point JPA is started and ready.
       
}
}
I always bound it asEagerSingleton, so it gets started right when the bindings are being processed

William Temperley

unread,
Jun 16, 2011, 11:44:24 AM6/16/11
to google...@googlegroups.com
Gabriel,

If you can use Guice-Servlet, running it through PersistFilter will start the persistence service.
Managing things this way is fantastically simple, and you get the benefit of request-scoped EntityManagers if you inject a provider of these into your servlets.

public class GuiceServletModule extends ServletModule {

  @Override
  protected void configureServlets() {
    install(new JpaPersistModule("p1"));   
    filter("/*").through(PersistFilter.class);
    serve("/gwtRequest").with(AppRequestFactoryServlet.class);
  }

Best regards,

Will

Gabriel Cardelli

unread,
Jun 17, 2011, 6:16:09 PM6/17/11
to google-guice
I Want use Provider<EntityManager> entityManager;

You have an example using Provider?


On Jun 16, 12:44 pm, William Temperley <willtemper...@gmail.com>
wrote:
> > On Wed, Jun 15, 2011 at 8:44 PM, jMotta <jayrmo...@gmail.com> wrote:
>
> >> Gabriel,
>
> >> I've never used Guice Persist, but when I've used any type of provider,
> >> including an entity manager provider, I had to bind it in a module, but once
> >> you're installing this JpaPersistModule this probably should work.
>
> >> Apparently there is something related to initiate the persistence service
> >> while using this JpaPersistModule, check this out in this link<http://novyden.blogspot.com/2011/03/hibernate-persistence-with-jpa-us...>
> >> .
>
> >> Let us know if it helps you! :)
>
> >> *Jayr Motta*
> >> Software Developer
> >> *
> >> *
> >> I'm  on BlackBeltFactory.com<http://www.blackbeltfactory.com/ui#%21User/jmotta/ref=jmotta>
> >> !

Gabriel Cardelli

unread,
Jul 3, 2011, 8:06:43 PM7/3/11
to google...@googlegroups.com
I'm trying execute Guice + JPA but i get an error:

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

1 error
	com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
	br.impa.apps.eventos.infra.dao.impl.EventoDAOImpl.save(EventoDAOImpl.java:31)
	br.impa.apps.eventos.infra.dao.impl.EventoDAOImpl.save(EventoDAOImpl.java:23)
	br.impa.apps.eventos.servico.impl.EventoServiceImpl.save(EventoServiceImpl.java:41)
	br.impa.apps.eventos.ws.EventoWS.list(EventoWS.java:30)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
	com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
	com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
	com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
	com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
	com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
	com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
	com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1465)
	com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1396)
	com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1345)
	com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1335)
	com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
	com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
	com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
	com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
	com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
	com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
	com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
	com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)

root cause

java.lang.NullPointerException
	com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:70)
	com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:50)
	com.google.inject.persist.jpa.JpaPersistService.get(JpaPersistService.java:34)
	com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:55)
	com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
	com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
	com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
	br.impa.apps.eventos.infra.dao.impl.EventoDAOImpl.save(EventoDAOImpl.java:31)
	br.impa.apps.eventos.infra.dao.impl.EventoDAOImpl.save(EventoDAOImpl.java:23)
	br.impa.apps.eventos.servico.impl.EventoServiceImpl.save(EventoServiceImpl.java:41)
	br.impa.apps.eventos.ws.EventoWS.list(EventoWS.java:30)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
	com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
	com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
	com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
	com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
	com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
	com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
	com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1465)
	com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1396)
	com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1345)
	com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1335)
	com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
	com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
	com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
	com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
	com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
	com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
	com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
	com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)

Classes:
public class GuiceServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        Injector i = Guice.createInjector(new ServletModule() {

            @Override
            protected void configureServlets() {
                // Bind classes
                bind(PerRequestResource.class);
                bind(EventoWS.class);
                bind(EventoService.class).to(EventoServiceImpl.class);
                bind(EventoDAO.class).to(EventoDAOImpl.class);
                bind(Principal.class).toProvider(PrincipalProvider.class);
                bind(SecurityContext.class).to(MySecurityContext.class);
                
                install(new JpaPersistModule("eventosImpa"));
                serve("/*").with(GuiceContainer.class);

                
            }
        });
        
        
        return i;
    }
}

DAO:

public class EventoDAOImpl implements EventoDAO {

	private final transient Logger log = Logger.getLogger(EventoDAOImpl.class);
	

	@Inject private Provider<EntityManager> entityManager;
	
	public EntityManager save(Evento object) {
		return entityManager.get();
	}
}

The method save return an error.

Thnks


William Temperley

unread,
Jul 4, 2011, 5:17:16 AM7/4/11
to google...@googlegroups.com
You're probably getting this error because you haven't called

install(new JpaPersistModule("eventosImpa"));

before binding any other classes that require an EntityManager. Just
move it to the top of your configure() method:

See "Web Environments" in:
http://code.google.com/p/google-guice/wiki/JPA

On 4 July 2011 01:06, Gabriel Cardelli


<gabrielcasti...@gmail.com> wrote:
> I'm trying execute Guice + JPA but i get an error:
>
> 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
>

>

> --
> 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/-/LvUn4p3OVkYJ.

Gabriel Cardelli

unread,
Jul 4, 2011, 7:06:11 AM7/4/11
to google...@googlegroups.com
I changed the order and got the same error.
I added now this line:
 
filter("/*").through(PersistFilter.class);
 
but i got the error:
 
INFO: Deploying web application archive jersey-guice-filter.war
04/07/2011 08:05:03 org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\Program Files\Apache Software Foundation\Tomcat 7.0\web
apps\jersey-guice-filter\WEB-INF\lib\tomcat-servlet-api-7.0.12.jar) - jar not lo
aded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servle
t.class
04/07/2011 08:05:05 org.apache.catalina.core.StandardContext startInternal
GRAVE: Error filterStart
04/07/2011 08:05:05 org.apache.catalina.core.StandardContext startInternal
GRAVE: Context [/jersey-guice-filter] startup failed due to previous errors
04/07/2011 08:05:05 org.apache.catalina.loader.WebappClassLoader clearReferences
Threads
GRAVE: The web application [/jersey-guice-filter] appears to have started a thre
ad named [com.google.inject.internal.util.$Finalizer] but has failed to stop it.
 This is very likely to create a memory leak.
 
 
please help.
 
thx.

Gabriel Cardelli

unread,
Jul 4, 2011, 8:19:08 AM7/4/11
to google...@googlegroups.com
William, can i use PersistFilter to start the service?

 

Att.


2011/6/16 William Temperley <willte...@gmail.com>

William Temperley

unread,
Jul 4, 2011, 9:21:21 AM7/4/11
to google...@googlegroups.com
I think this error may not be Guice related, I think you're trying to
load a servlet spec that's higher than the container you are running.
Tomcat 7 supports servlet 3.0, if you're using GWT dev mode, it's
running Jetty6 with servlet 2.5.

In reply to your other mail, yes you should use PersistFilter for all
your servlets that you wish to utilize Guice provided JPA classes.


On 4 July 2011 12:06, Gabriel Cardelli

> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.

Gabriel Cardelli

unread,
Jul 4, 2011, 9:48:29 AM7/4/11
to google...@googlegroups.com
Dear William,
 
I'm using Google Guice + Jersey only to expose Web Services, that other application will consume.
 
When i used PersistFilter, i got that error.
 


2011/7/4 William Temperley <willte...@gmail.com>
Reply all
Reply to author
Forward
0 new messages