1

I have Dao object that i want to inject EntityManager into :

    @Transactional
    public class ConfigEntryDao implements IDao<ConfigEntryEntity> {

        //THIS IS WHAT I WANT TO DO!
        @PersistenceContext(unitName = XChangeUtil.JPA.JPA_PU_NAME)
        private EntityManager entityManager;
        //ONLY THIS ONE WORKS!
        //private EntityManagerFactory emf = Persistence.createEntityManagerFactory(XChangeUtil.JPA.JPA_PU_NAME);
        //private EntityManager entityManager = emf.createEntityManager();
        //....Other methods bellow

If i use commented out version with entity manager factory without injection , i can get my code to work , everything works fine, bud once i use @Persistence context, i get exception :

14:18:15,854 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC000001: Failed to start service jboss.deployment.unit."xChange-1.0-SNAPSHOT.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."xChange-1.0-SNAPSHOT.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1978)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:114)
    at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:77)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:350)
    at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:338)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:216)
    at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:188)
    at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:448)
    at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:96)
    at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:59)
    at org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:49)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:63)
    at org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:47)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:113)
    at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:86)
    at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:100)
    at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:80)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:261)
    at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:226)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:74)
    at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:71)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

14:18:15,858 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 3) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "xChange-1.0-SNAPSHOT")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xChange-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
    Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager"}}
14:18:15,858 ERROR [org.jboss.as.server] (management-handler-thread - 3) WFLYSRV0021: Deploy of deployment "xChange-1.0-SNAPSHOT.war" was rolled back with the following failure message: 
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xChange-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
    Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager"}}
14:18:15,862 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0019: Stopped Driver service with driver-name = xChange-1.0-SNAPSHOT.war_org.apache.derby.jdbc.AutoloadedDriver_10_12
14:18:15,862 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) WFLYJCA0019: Stopped Driver service with driver-name = xChange-1.0-SNAPSHOT.war_com.mysql.cj.jdbc.Driver_6_0
14:18:15,982 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0028: Stopped deployment xChange-1.0-SNAPSHOT (runtime-name: xChange-1.0-SNAPSHOT.war) in 122ms
[2018-01-28 02:18:16,041] Artifact xChange:war exploded: Error during artifact deployment. See server log for details.
[2018-01-28 02:18:16,041] Artifact xChange:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xChange-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
    Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'eclipselinkPu' in deployment xChange-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager com.bisciak.xchange.dao.ConfigEntryDao.entityManager"}}

Interesting thing is - If i remove beans.xml form my project, i can use @PersistenceContext annotation and deploy successfully, bud then i have problems with CDI. My beans.xml has nothign in it beside bean discovery all:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>

My structure of project + persistence.xml: enter image description here

This is the usage of my ConfigEntryDao:

    public class ConfigurationService {

    @Inject
    private ConfigEntryDao configEntryDao;

    private List<ConfigEntryEntity> configEntries;



    @PostConstruct
    public void initialize() {
        System.out.println("DBG Constructed");
        configEntryDao.insert(new ConfigEntryEntity(ConfigNamespace.xchange_test, "Hello"));
        configEntryDao.insert(new ConfigEntryEntity(ConfigNamespace.xchange_test_subtest, "World"));
        configEntries = configEntryDao.getAll();
    }

    public void tryTestValue() {
        System.out.println("Hello");
        System.out.println("Testing data from config db table Size: "+configEntries.size()+" Values:"+ configEntries.toString());
    }

}

And i test the method from REST endpoint since im too cool to use UI:

@Path("/test")
public class TestResource {

    @Inject
    private ConfigurationService configurationService;

    @GET
    @Path("/dbtest")
    public Response testDb() {
        System.out.println("DBG Invocation of dbtest");
        configurationService.tryTestValue();
        return Response.status(Response.Status.OK).build();
    }

}

So the question is : Anyone knows why i cant inject entity manager via PersistenceContext?

Note: I use latest stable Wildfly

Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
  • Are you absolutely sure that you're using correct unitName? It should be `The name of the persistence unit as defined in the persistence.xml file.` – asm0dey Jan 28 '18 at 16:05
  • @asm0dey its 100% same... – Tomas Bisciak Jan 28 '18 at 16:14
  • You have a JTA Persistence Unit but not defined a JDBC datasource. I assume that your persistence unit is not created. Have you checked the logs? – Simon Martinelli Jan 28 '18 at 16:43
  • @Simon Martinelli doesnt change anything when i specify jta datasource, I added it just to test, nothing changed, still same error. – Tomas Bisciak Jan 28 '18 at 17:13
  • You say that you are using EclipseLink but Wildfly contains Hibernate – Simon Martinelli Jan 28 '18 at 17:18
  • Yep, i use eclipselink for these properties, since i copied it from somewhere, bud i have hibernate-core latest as dependency as well, not sure if needed. As far as i know, provider will be selected by default, which one suits best. – Tomas Bisciak Jan 28 '18 at 17:22
  • add the provider tag to make it clear to the server which provider should be used, and then turn on logging for that provider to see what might be going wrong or if it even gets to trying to load the persistence unit. org.eclipse.persistence.jpa.PersistenceProvider is used for EclipseLink since you already have it configured to log at the fine level. – Chris Jan 29 '18 at 15:29
  • @Chris hi Chris , please check my most recent question, i will try what you have suggested there: https://stackoverflow.com/questions/48503545/cant-inject-entitymanager-with-persistencecontext-in-most-basic-example – Tomas Bisciak Jan 29 '18 at 15:50
  • @Chris how do you turn on logging for provider? – Tomas Bisciak Jan 29 '18 at 16:40
  • Its provider specific, so you would want to specify the provider or the container uses what ever it wants. For EclipseLink, see https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging but you already have fine logging specified using the logging.level property. – Chris Jan 29 '18 at 17:01
  • @Chris i cant see any output regarding this logging. I dont think persistence.xml settings has any effect. It just doesnt see it. – Tomas Bisciak Jan 29 '18 at 17:23

1 Answers1

1

Your ConfigEntryDao should be managed by the container in order to let the container inject the EntityManager. Make it for example a stateless session bean (SLSB) by annotating it with @Stateless.

See also here for how to create a producer for the EntityManager.

siom
  • 1,610
  • 1
  • 14
  • 21