Briston12
Briston12

Reputation: 377

Quarkus dependency app using different persistence unit triggering errors related to EntityManager

I've got the following class inside a primary quarkus application (named dep-repos) I've created:

package com.myapps.dep.common;

@ApplicationScoped
public class DepRepository implements PanacheRepositoryBase<DepEntity, Integer> {

    @Inject
    @PersistenceUnit("dep")
    EntityManager em;

    @Transactional
    public void save(final List<DepEntity> entities) {
        entities.forEach(e -> em.persist(e));
    }
}

that I use this way:

package com.myapps.dep.common;

@ApplicationScoped
public class MyDep {

    @Inject
    DepRepository repository;

    // ...
}

and I use that app as dependency of another quarkus app:

<dependency>
   <groupId>com.myapps<groupId>
   <artifactId>dep-repos</artifactId>
   <version>1.0.0/version>
</dependency>

that I use this way:

package com.myapps.main;

import com.myapps.dep.common.MyDep;

@ApplicationScoped
public class MyServiceImpl implements MyService {

    @Inject
    MyDep myDep;

    // ...
}

and I defined all the required properties inside a application.yml file, where I now have 2 persistence units, one for the main app and the other one releated to the other dep-repos app:

quarkus:
  datasource:
    db-kind: db2
    username: test
    password: test
    jdbc:
      url: jdbc:db2://<url>
    "dep":
      db-kind: db2
      username: test
      password: test
      jdbc:
        url: jdbc:db2://<url>
  hibernate-orm:
    dialect: org.hibernate.dialect.DB2Dialect
    database:
      generation: validate
    packages: com.myapps.main
    "dep":
      datasource: dep
      packages: com.myapps.dep

now, when I build the application:

./mvnw package -Dquarkus.config.locations=file:/./application.yml -Pnative

I get the following errors:

[ERROR] Failed to execute goal io.quarkus.platform:quarkus-maven-plugin:2.12.2.Final:build (default) on project my-app-main: Failed to build quarkus application: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[ERROR]         [error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type javax.persistence.EntityManager and qualifiers [@PersistenceUnit(value = "dep")]
[ERROR]         - java member: com.myapps.dep.common.DepRepository#em
[ERROR]         - declared on CLASS bean [types=[io.quarkus.hibernate.orm.panache.PanacheRepositoryBase<com.myapps.dep.common.DepEntity, java.lang.Integer>, java.lang.Object, com.myapps.dep.common.DepRepository], qualifiers=[@Default, @Any], target=com.myapps.dep.common.DepRepository]
[ERROR]         The following beans match by type, but none have matching qualifiers:
[ERROR]                 - Bean [class=org.hibernate.Session, qualifiers=[@javax.enterprise.inject.Default, @Any]]
[ERROR]         at io.quarkus.arc.processor.BeanDeployment.processErrors(BeanDeployment.java:1215)
[ERROR]         at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:275)
[ERROR]         at io.quarkus.arc.processor.BeanProcessor.initialize(BeanProcessor.java:140)
[ERROR]         at io.quarkus.arc.deployment.ArcProcessor.validate(ArcProcessor.java:496)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[ERROR]         at io.quarkus.deployment.ExtensionLoader$3.execute(ExtensionLoader.java:909)
[ERROR]         at io.quarkus.builder.BuildContext.run(BuildContext.java:281)
[ERROR]         at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
[ERROR]         at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
[ERROR]         at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
[ERROR]         at java.base/java.lang.Thread.run(Thread.java:829)
[ERROR]         at org.jboss.threads.JBossThread.run(JBossThread.java:501)
[ERROR] Caused by: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type javax.persistence.EntityManager and qualifiers [@PersistenceUnit(value = "dep")]
[ERROR]         - java member: com.myapps.dep.common.DepRepository#em
[ERROR]         - declared on CLASS bean [types=[io.quarkus.hibernate.orm.panache.PanacheRepositoryBase<com.myapps.dep.common.DepEntity, java.lang.Integer>, java.lang.Object, com.myapps.dep.common.DepRepository], qualifiers=[@Default, @Any], target=com.myapps.dep.common.DepRepository]
[ERROR]         The following beans match by type, but none have matching qualifiers:
[ERROR]                 - Bean [class=org.hibernate.Session, qualifiers=[@javax.enterprise.inject.Default, @Any]]
[ERROR]         at io.quarkus.arc.processor.Beans.resolveInjectionPoint(Beans.java:411)
[ERROR]         at io.quarkus.arc.processor.BeanInfo.init(BeanInfo.java:536)
[ERROR]         at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:263)
[ERROR]         ... 13 more
[ERROR] -> [Help 1]

Any idea on how to fix that?

Thanks

Upvotes: 0

Views: 726

Answers (1)

Christian Beikov
Christian Beikov

Reputation: 16430

I'm not 100% sure how this works, but could you try the following?

@ApplicationScoped
public class DepRepository implements PanacheRepositoryBase<DepEntity, Integer> {

    @Inject
    @io.quarkus.hibernate.orm.PersistenceUnit("dep")
    EntityManagerFactory emf;
    @Inject
    EntityManager em;

    @Transactional
    public void save(final List<DepEntity> entities) {
        entities.forEach(e -> em.persist(e));
    }
}

Upvotes: 0

Related Questions