WELD-001408 Unsatisfied dependencies when injecting EntityManager
Asked Answered
W

2

7

I have @Stateless bean which implements two interfaces (remote and local). I have also added @LocalBean anotation for accessing bean with a no-interface view.

@Stateless
@LocalBean
public class WeatherDataBean implements WeatherDataBeanRemote, WeatherDataBeanLocal {
    @Inject
    private EntityManager entityManager;

    public WeatherDataBean () {

    }
    // ....attributes, getter & setter methods ....
}

I use @Inject for this reason taken from this example of JBoss AS7 quickstart:

We use the "resource producer" pattern, from CDI, to "alias" the old fashioned @PersistenceContext injection of the entity manager to a CDI style injection. This allows us to use a consistent injection style (@Inject) throughout the application.

Now previously I have used:

@PersistenceContext(unitName="WeatherStationJPA")
private EntityManager entityManager;

In EJB and it works without any problem. But with @Inject annotation I get this error:

WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private ejb.WeatherDataBean.entityManager]

Here is how I have class Resources defined:

public class Resources {
     @SuppressWarnings("unused")
     @PersistenceContext(unitName="WeatherStationJPA")
     @Produces
     private EntityManager entityManager;

     @Produces
     FacesContext getFacesContext() {
         return FacesContext.getCurrentInstance();
     }
}

Why Is that I get this error if I try to inject entity manager?

EDIT: On request from @LightGuard I am adding packages that I am using to reference annotations:

  1. WeatherDataBean has:

    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    import javax.inject.Inject;
    
  2. Resources has:

    import javax.enterprise.inject.Produces;
    import javax.faces.context.FacesContext;
    import javax.inject.Singleton;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
W answered 31/7, 2012 at 19:58 Comment(3)
Everything looks correct. Are you sure you have the correct annotations (check packages)? Also may depend on how you're packaging things.Axinomancy
Please check my answer I have added which packages I use for these annotations. I am using EAR project to package EJB project, EJBClient project, JPA project and JSF project.W
Ah, okay if you're using an EAR things change up a bit. You'll have to place where you're doing the injection, which library the classes are in and where your beans.xml files are located.Axinomancy
P
7

I just had the same problem, I fixed this by adding this class to my project

import java.util.logging.Logger;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans
 * 
 * <p>
 * Example injection on a managed bean field:
 * </p>
 * 
 * <pre>
 * &#064;Inject
 * private EntityManager em;
 * </pre>
 */
public class Resources {
   // use @SuppressWarnings to tell IDE to ignore warnings about field not being referenced directly
   @SuppressWarnings("unused")
   @Produces
   @PersistenceContext
   private EntityManager em;

  // @Produces
  // public Logger produceLog(InjectionPoint injectionPoint) {
   //   return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
  // }

   @Produces
   @RequestScoped
   public FacesContext produceFacesContext() {
      return FacesContext.getCurrentInstance();
   }

}
Purree answered 11/5, 2013 at 19:33 Comment(0)
L
7

I have ran into a similar error, turned out I didn't put a beans.xml file in the WEB-INF folder. The beans.xml file can be an empty file sitting in the WEB-INF folder. JBossAS checks that file to start the CDI service.

Laise answered 20/5, 2013 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.