Search code examples
javajerseyhk2

Jersey + HK2: EntityManager injection fails


I'm following this advice to get an EntityManager injected into my Jersey + HK2 project. For some reason, I suddenly see this exception when starting the service:

Exception in thread "main" java.lang.IllegalArgumentException: Creation of FactoryDescriptors must have Factory as a contract of the first argument at org.glassfish.hk2.utilities.FactoryDescriptorsImpl.(FactoryDescriptorsImpl.java:78) at org.glassfish.hk2.utilities.binding.AbstractBindingBuilder$FactoryTypeBasedBindingBuilder.complete(AbstractBindingBuilder.java:453) at org.glassfish.hk2.utilities.binding.AbstractBinder.resetBuilder(AbstractBinder.java:180) at org.glassfish.hk2.utilities.binding.AbstractBinder.complete(AbstractBinder.java:190) at org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:174) at org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:187) ....

Here's my code:

EMFFactory

public class EMFFactory implements Factory<EntityManagerFactory> {
    private final Logger log = LoggerFactory.getLogger(EMFFactory.class);
    protected EntityManagerFactory emf;

    @Inject
    Config config;

    @PostConstruct
    public void setup() {
        Properties p = new Properties();
        p.put("javax.persistence.jdbc.url", config.getJdbcUrl());
        p.put("javax.persistence.jdbc.user", config.getJdbcUser());
        p.put("javax.persistence.jdbc.password", config.getJdbcPassword());
        emf = Persistence.createEntityManagerFactory("skp-server-PU", p);
        log.debug("JDBC URL: "+ config.getJdbcUrl());
    }

    @Override
    public EntityManagerFactory provide() {
        return emf;
    }

    @Override
    public void dispose(EntityManagerFactory instance) {}

}

EMFactory

public class EMFactory implements Factory<EntityManager> {
    private final Logger log = LoggerFactory.getLogger(EMFFactory.class);
    private EntityManager em;

    @Inject
    EntityManagerFactory emf;

    @PostConstruct
    public void setup() {
        em = emf.createEntityManager();
        log.debug("New EntityManager created");
    }

    @Override
    public EntityManager provide() {
        return em;
    }

    @Override
    public void dispose(EntityManager instance) {
        log.debug("Disposing of EntityManager");
    }

}

The ApplicationConfig binds the factories:

    ServiceLocatorUtilities.bind(applicationLocator, new AbstractBinder() {

        @Override
        protected void configure() {
            bindFactory(EMFFactory.class)
                    .to(EntityManagerFactory.class)
                    .in(Singleton.class);
            bindFactory(EMFactory.class)
                    .to(EntityManager.class);
        }
    });

Can someone explain the exception?


Solution

  • Not sure if it will ever help anyone, but I found out how I broke it:

    I am creating a shaded uber-jar with the maven shade plugin. The plugin was complaining about overlapping classes, so I excluded the following package from being shaded:

    <!-- This one comes with epcliselink, but I don't want shaded, hence the scope -->
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
        <scope>provided</scope>
    </dependency>
    

    That, my friends, was not a good idea. Removing the section fixed the problem.