0 Replies Latest reply on Mar 18, 2013 9:30 AM by Wesley Janik

    @Inject EntityManager Fails

    Wesley Janik Newbie

      Hello-

       

      I'm using AS7.1.1, packaging a bunch of EJBs within a WAR deployment, and am having trouble adding in CDI functionality.  The EJBs will be packaged in separate jar files, but I want to share a persistence unit between them.  To accomplish that sharing, I have a persistence.jar file containing nothing but a persistence.xml in its META-INF.

       

      The problem comes in when I try to use @Inject to get an entity manager in the EJBs while including a beans.xml in the EJB jar files to "activate" CDI.  If I use @PersistenceContext, injection works fine.  If I use @Inject but don't include the beans.xml, injection works just fine.  The combination of the two, however, gives me this error message:

      ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.deployment.unit."sample-with-beansxml.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."sample-with-beansxml.war".WeldService: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private org.sample.TestEjb.em]
      

       

      For that above error, I created a (very) small sample application.  It has one class, TestEjb, defined as follows:

      package org.sample;
      
      import javax.ejb.Stateless;
      import javax.inject.Inject;
      import javax.persistence.EntityManager;
      
      @Stateless
      public class TestEjb
      {
          @Inject
          private EntityManager em;
      }
      

       

      I also have a persistence.xml defined as follows:

      <?xml version="1.0" encoding="UTF-8" ?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                   version="1.0">
      
          <persistence-unit name="persistence-unit">
              <jta-data-source>java:/datasources/DS</jta-data-source>
              <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
              </properties>
          </persistence-unit>
      </persistence>
      

       

      The whole thing is packaged as follows:

      [sample-with-beansxml.war]

      /WEB-INF

      /lib

      [persistence.jar]

      /META-INF

      persistence.xml

      [ejb-with-beansxml.jar]

      /META-INF

      beans.xml (empty)

      /org

      /sample

      TestEjb.class

       

      If I take that same deployment and pull the beans.xml out of the ejb jar, it all works fine.

       

      I'm assuming I'm missing something, but I can't seem to figure out what it is.  Am I trying to mix two things that don't mix here?

       

       

      EDIT:

       

      I thought the problem was with a shared entity manager, but it's not.  It doesn't seem to work at all in AS7.1.1Final.  I repackaged the above to:

      [sample-with-beansxml.war]

      /WEB-INF

      /lib

      [ejb-with-beansxml.jar]

      /META-INF

      beans.xml (empty)

      persistence.xml

      /org

      /sample

      TestEjb.class

       

      And I still get the same error!

       

      Message was edited by: Wesley Janik