dependency-injectionjava-ee-6cdientitymanagerstateless-session-bean

WELD-001408 Unsatisfied dependencies when injecting EntityManager


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;
    

Solution

  • 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();
       }
    
    }