quarkus: EntityManager is not injected

Describe the bug EntityManager instance is not injected into @ApplicationScoped object so i’m having the following exception for ``:

...
12:02:31,107 ERROR [io.qua.dev.DevModeMain] Failed to start Quarkus: java.lang.ExceptionInInitializerError
...
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property entityManager has not been initialized
...

Expected behavior EntityManager instance is injected and ready -to-use, no exception are thrown

Actual behavior during ./mvnw compile quarkus:dev i have the following exception

...
12:02:31,107 ERROR [io.qua.dev.DevModeMain] Failed to start Quarkus: java.lang.ExceptionInInitializerError
...
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property entityManager has not been initialized
...

The app is shutdown.

To Reproduce Steps to reproduce the behavior:

  1. git clone git@github.com:4ntoine/NotesServerApp.git
  2. cd NotesServerApp
  3. git checkout 32c8240dfd6672471cdd73f613e092522fb6528e
  4. ./gradlew publishToMavenLocal
  5. cd app-quarkus
  6. ./mvnw compile quarkus:dev

Configuration

quarkus.datasource.url = jdbc:h2:mem:notes
quarkus.datasource.driver = org.h2.Driver

# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation=drop-and-create

Screenshots Снимок экрана 2019-11-16 в 12 07 33

Environment (please complete the following information):

  • Output of uname -a or ver:

Darwin MBP-Anton.local 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64

  • Output of java -version:
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
  • GraalVM version (if different from Java):
  • Quarkus version or git rev:

Additional context

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

It turns out to be a problem with how CDI is being used in the supplied code:

If you use something like:

@ApplicationScoped
open class QuarkusEntityManagerFactory {
    @Inject
    lateinit var entityManager: EntityManager
}

@ApplicationScoped
open class Configuration {

    @Produces
    open fun getJpaGateway(entityManagerFactory: QuarkusEntityManagerFactory): JpaGateway {
        val mapper = MapperImpl()
        return JpaGateway(entityManagerFactory.entityManager, mapper)
    }

    @Produces
    open fun getAddNoteUseCase(gateway: JpaGateway) = AddNoteImpl(gateway)

    @Produces
    open fun getListNotesUseCase(gateway: JpaGateway): ListNotes = ListNotesImpl(gateway)
}

everything works as expected.

Note that you can simplify things even more by removing QuarkusEntityManagerFactory completely and using something like:

    @Produces
    open fun getJpaGateway(entityManager: EntityManager): JpaGateway {
        val mapper = MapperImpl()
        return JpaGateway(entityManager, mapper)
    }

So I’m going to go ahead and close this since it’s not a Quarkus bug. If you feel that this resolution is not correct, feel free to reopen and add more information.

Thanks

Thank you. Let me know if you need any assistance

пн, 18 нояб. 2019 г., 14:49 Georgios Andrianakis notifications@github.com:

I was able to reproduce the problem. I’ll update with more information soon.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/quarkusio/quarkus/issues/5528?email_source=notifications&email_token=AAGOWT6XF37ZDVKNU65NIQTQUJQMFA5CNFSM4JOD46X2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEJ3GIY#issuecomment-554939171, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGOWT6E24SCECSTPUAP223QUJQMFANCNFSM4JOD46XQ .