Search code examples
javajersey-2.0hk2

Configure EntityManager in a Jersey / hk2 application


I'm trying to inject EntityManagerFactory (as a Singleton) and EntityManager (in a request scope) in my Jersey / Hk2 application.

I'm using this question (How to i properly configure an entitymanager in a jersey-hk2 application) as a guide

I spent one day trying to get it running but I got a lot of exceptions. I don't know where the error is.

WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object 
available for injection at SystemInjecteeImpl(requiredType=EntityManagerFactory,parent=EntityManagerProvider...)
...
While attempting to resolve the dependencies of mypackages.EntityManagerProvider errors were found
...
java.lang.IllegalStateException: Unable to perform operation: resolve on mypackages.EntityManagerProvider
...
There was no object available for injection at SystemInjecteeImpl(requiredType=EntityManagerFactory,parent=EntityManagerProvider...
...
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of mypackages.EntityManagerProvider errors were found
...
java.lang.IllegalStateException: Unable to perform operation: resolve on mypackages.EntityManagerProvider

And a lot of exceptions that I cut

It's my code:

My EntityManagerFactoryProvider (Singleton):

public class EntityManagerFactoryProvider implements Factory<EntityManagerFactory> {

private static final String PERSISTENT_UNIT = "test";
private final EntityManagerFactory emf;

public EntityManagerFactoryProvider(@Named(PERSISTENT_UNIT) String persistentUnit) {
    emf = Persistence.createEntityManagerFactory(persistentUnit);
}
@Override
public EntityManagerFactory provide() {
    return emf;
}
@Override
public void dispose(EntityManagerFactory instance) {
    instance.close();   
}

} 

My EntityManagerProvider (per request) is:

public class EntityManagerProvider implements Factory<EntityManager> {

private final EntityManagerFactory emf;
private final CloseableService closeableService;

@Inject
public EntityManagerProvider(EntityManagerFactory emf, CloseableService closeableService) {
    this.emf = emf;
    this.closeableService = closeableService;   
}
@Override
public EntityManager provide() {
    final EntityManager em = emf.createEntityManager();
    this.closeableService.add(new Closeable() {
        @Override
        public void close() throws IOException {
            em.close();
        }
    });
    return em;
}
@Override
public void dispose(EntityManager entityManager) {
    if (entityManager.isOpen()) {
        entityManager.close();
    }
}

}

MyApplication is:

public class MyApplication extends ResourceConfig {

public MyApplication() {
    register(new MyApplicationBinder());
    packages(true, "mypackages");
}

}

MyApplicationBinder is:

 public class MyApplicationBinder extends AbstractBinder {

@Override
protected void configure() {
bindFactory(EntityManagerFactoryProvider.class)
   .to(EntityManagerFactory.class)
   .in(Singleton.class);
bindFactory(EntityManagerProvider.class)
   .to(EntityManager.class)
   .in(RequestScoped.class);
}
}

And My Resource is:

@Path("myresource")
public class MyResource {

@Inject
EntityManager em;

/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
    return "Got it!";
}

}

And Web.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!-- This web.xml file is not required when using Servlet 3.0 container,
 see implementation details 
 http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>MyApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
    class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>mypackage.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyApplication</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

Thanks a lot


Solution

  • I don't think you are showing the complete stacktrace. When testing, I do see that part of the trace you are showing in your post, but it is only part of the trace. The main cause is

    java.lang.NoSuchMethodException: Could not find a suitable constructor in EntityManagerFactoryProvider

    Look at your EntityManagerFactoryProvider constructor.

    public EntityManagerFactoryProvider(@Named(PERSISTENT_UNIT) String persistentUnit) {
        emf = Persistence.createEntityManagerFactory(persistentUnit);
    }
    

    Where is the persistentUnit supposed to come from. You need to hook it up in the binder. And also @Named does not define an injection point. It is only a support annotation for an injection point. An injection point it created when you @Inject just like you did on the EntityManagerProvider constructor.

    So fix two things:

    1. Bind the persistence unit name

      bind(persistenceUnitName).to(String.class).named(PERSISTENT_UNIT);
      
    2. Create the injection point by adding the @Inject annotation

      @Inject
      public EntityManagerFactoryProvider(@Named(PERSISTENT_UNIT) String persistentUnit) {
          emf = Persistence.createEntityManagerFactory(persistentUnit);
      }