0 Replies Latest reply on Sep 1, 2012 1:06 PM by Cyrill Rüttimann

    Unable to @Inject EntityManager in EAR-Deployment

    Cyrill Rüttimann Newbie

      Hello,

       

      I have searched several discussions on this topic but did not found the ultimate solution.

       

      Let me explain my setup. I have a single ear deployed to Weblogic 12c. The EAR contains two EJB-JARS with Entity-Beans, Stateless Facades and various CDI-Managed beans like Repositories and Services. Each EJB-JAR has its own persistence.xml pointing to the same connection. I have one common archive packaged in APP-INF/lib. This common archive contains the PersistenceContextProducer and the Qualifier. When I ask the BeanManager, I can see the Producer declared as a Resource Provider Field:

       

      org.jboss.weld.bean-jee6skeletonserver.jar_EJB_JAR-Built-in-javax.enterprise.context.Conversation,
      Managed Bean [class org.happycode.jee6skeleton.persistence.PersistenceProducer] with qualifiers [@Any @Default],
      Built-in Bean [java.security.Principal] with qualifiers [@Default],
      Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default],
      Managed Bean [class org.happycode.jee6skeleton.cru.domain.person.PartnerService] with qualifiers [@Any @Default],
      Session bean [class org.happycode.jee6skeleton.cru.application.person.PersonService with qualifiers [@Any @Default]; local interfaces are [PersonService],
      Resource Producer Field [EntityManager] with qualifiers [@Any @BusinessDatabase] declared as [[field] @Produces @PersistenceContext @BusinessDatabase private org.happycode.jee6skeleton.persistence.PersistenceProducer.em],
      Session bean [class org.happycode.jee6skeleton.cru.application.person.boundary.PersonServiceRMIClient with qualifiers [@Any @Default]; local interfaces are [],
      Managed Bean [class org.happycode.jee6skeleton.cru.domain.person.PersonRepository] with qualifiers [@Any @Default],
      Implicit Bean [javax.enterprise.event.Event] with qualifiers [@Default],
      Built-in Bean [javax.transaction.UserTransaction] with qualifiers [@Default],
      Built-in Bean [javax.enterprise.inject.spi.BeanManager] with qualifiers [@Default],
      Managed Bean [class org.happycode.jee6skeleton.cru.domain.person.historisiert.PeriodenPersonRepository] with qualifiers [@Any @Default],
      Built-in Bean [javax.validator.Validator] with qualifiers [@Default],
      Built-in Bean [javax.validator.ValidatorFactory] with qualifiers [@Default],
      Managed Bean [class org.happycode.jee6skeleton.cru.domain.person.Partnerschaft] with qualifiers [@Any @Default],
      Implicit Bean [javax.enterprise.inject.spi.InjectionPoint] with qualifiers [@Default]]
      

       

      The Producer packaged in the common archive in APP-INF/lib is implemented like this

       

       

      public class PersistenceProducer {
      
          @Produces
          @BusinessDatabase
          @PersistenceContext(name = "happycode")
          EntityManager entityManager;
      }
      

       

      and used in a Repository like this:

       

       

      public class PersonRepository {
      
          @Inject
          @BusinessDatabase
          EntityManager entityManager;
      
      
          public List<Person> loadPersonsByNachname(final String nachname) {
              System.out.println("PersonRepository.loadPersonsByNachname()");
      
              CriteriaBuilder criteriaBuilder = entityManager2.getCriteriaBuilder();
              CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class);
      
              // Metamodel
              Root<Person> person = criteriaQuery.from(Person.class);
      
              // Criteria
              criteriaQuery.where(criteriaBuilder.like(person.get(Person_.nachname), nachname));
      
              TypedQuery<Person> query = entityManager2.createQuery(criteriaQuery);
      
              System.out.println("loadPersonsByNachname, executing query");
              return query.getResultList();
          }
      

       

      Whenever the EntityManager is accessed, the following log message appears:

       

       

      <01.09.2012 17:04 Uhr MESZ> <Info> <EJB> <BEA-010227> <EJB exception occurred during invocation from home or business: weblogic.ejb.container.internal.StatelessEJBLocalHomeImpl@22a9e58 generated exception: org.jboss.weld.exceptions.NullInstanceException: WELD-000044 Unable to obtain instance from null> 
      

       

      Is it possible that the cause is, that the producer does not live in the same archive as the persistence.xml which is contained in the EJB-JAR?

      Or do you have any other hints how to resolve this issue?

       

      Thanks,

       

      Cyrill