Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsatisfied dependencies for type EntityManager with qualifiers @Default

My project structure looks like

project/
  business/pom.xml
  services/pom.xml
  integration/pom.xml

business/pom.xml

    <dependencies>
        <dependency>
            <groupId>com.self</groupId>
            <artifactId>persistence</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

persistence/pom.xml

    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>${hibernate-jpa-2.0-api.version}</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl-version}</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>${querydsl-version}</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>${querydsl-version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-entitymanager.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

project/pom.xml

    <dependencies>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>${jsr305.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee-api.version}</version>
        </dependency>
    </dependencies>

business/ClientRegistrationManager.java

@Stateless
public class ClientRegistrationManager {
    private EntityManager entityManager;

    @SuppressWarnings("UnusedDeclaration")
    public ClientRegistrationManager() {
    }

    @Inject
    public ClientRegistrationManager(@Nonnull final EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public ClientDetail register(@Nonnull final String email, final long id, @Nonnull final String password) {
        final UserService userService = new UserService(entityManager);
        final User user = userService.getUserByEmail(email);
        return new ClientDetail("clientId", "clientSecret");
    }
}  

When I deploy, I see the following error

[INFO] [talledLocalContainer] 17:48:53,405 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-15) MSC000001: Failed to start service jboss.deployment.unit."application.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."application.war".WeldStartService: Failed to start service
[INFO] [talledLocalContainer]   at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
[INFO] [talledLocalContainer]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer]   at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer] Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
[INFO] [talledLocalContainer]   at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.self.oauth.business.oauth.ClientRegistrationManager(@Nonnull EntityManager)
[INFO] [talledLocalContainer]   at com.self.oauth.business.oauth.ClientRegistrationManager.<init>(ClientRegistrationManager.java:0)
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:368)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:289)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:135)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:166)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:514)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
[INFO] [talledLocalContainer]   at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
[INFO] [talledLocalContainer]   at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
[INFO] [talledLocalContainer]   at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer]   ... 3 more
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer] 17:48:53,411 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "application.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"application.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"application.war\".WeldStartService: Failed to start service
[INFO] [talledLocalContainer]     Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
[INFO] [talledLocalContainer]   at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.self.oauth.business.oauth.ClientRegistrationManager(@Nonnull EntityManager)
[INFO] [talledLocalContainer]   at com.self.oauth.business.oauth.ClientRegistrationManager.<init>(ClientRegistrationManager.java:0)
[INFO] [talledLocalContainer] "}}

I also added empty beans.xml at the following locations

business/src/main/resources/META-INF/beans.xml
services/src/main/resources/META-INF/beans.xml
services/src/main/webapp/WEB-INF/beans.xml

But none of them helped

Can you please tell me what am I missing?

like image 517
daydreamer Avatar asked Aug 09 '14 00:08

daydreamer


2 Answers

We normally inject EntityManager through '@PersistenceContext(unitname="name")' In order for you to use the constructor injection for the EntityManager, you would probably need to define a producer method.

@Stateless
public class MyEntityManagerProducer{

  @PersistenceContext(unitname="name")
  private EntityManager entityManager;

  @Produces
  public EntityManager entityManager(){
    return entityManager;
  }

}

And then within your constructor injection:

  @Inject
    public ClientRegistrationManager(@Nonnull final EntityManager entityManager) {
        this.entityManager = entityManager;
    }
like image 148
maress Avatar answered Sep 17 '22 23:09

maress


There is no default producer method for EntityManager. You need to create one to use @Inject.

like image 40
John Ament Avatar answered Sep 19 '22 23:09

John Ament