question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EntityManager is not injected

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
geoandcommented, Nov 18, 2019

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

1reaction
4ntoinecommented, Nov 18, 2019

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 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why EntityManager is not injected? - java - Stack Overflow
Injection of EntityManagers only works in managed beans, since you are creating the Finder with new no container is involved.
Read more >
JPA problem - EntityManager is not injected - Forums - IBM
I have made the Liberty JPA tutorial to work, but now I am rebuilding the app using injected JTA EntityManager instead of using...
Read more >
EntityManager in not injected when using devmode with gradle
I'm using JPA + Hibernate without Panache. application.properties seems to be configured correctly. Beans injecting annotations are set. However ...
Read more >
EntityManager is not injected inside a Jboss module
I'm working with jboss wildfly 9. I have a provider deployed as module into the modules directory. Then i have a jpa project...
Read more >
Injection Of Entitymanager - Apache TomEE
This particular EntityManager is injected as an EXTENDED persistence context, which simply means that the EntityManager is created when the @Stateful bean is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found