Quarkus: EntityManager in not injected when using devmode with gradle

Created on 14 Nov 2019  路  25Comments  路  Source: quarkusio/quarkus

Describe the bug
I'm using JPA + Hibernate without Panache. application.properties seems to be configured correctly. Beans injecting annotations are set. However EntityManager instance is not injected.

Expected behavior
EntityManager instance is instantiated by Quarkus (Arc) and injected.

Actual behavior
EntityManager instance is null.

To Reproduce
Steps to reproduce the behavior:

  1. git clone [email protected]:4ntoine/NotesServerApp.git
  2. git checkout issue-5-quarkus
  3. ./gradlew :app-quarkus:quarkusDev
  4. navigate to "localhost:8080/hello" and make sure you can see stacktrace NPE''

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
(If applicable, add screenshots to help explain your problem.)
小薪懈屑芯泻 褝泻褉邪薪邪 2019-11-15 胁 0 05 26

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: 1.0.0CR1

Additional context
CDI configuration:

package name.antonsmirnov.notes

import name.antonsmirnov.notes.gateway.JpaGateway
import name.antonsmirnov.notes.gateway.mapper.MapperImpl
import name.antonsmirnov.notes.usecase.*
import javax.enterprise.inject.Produces
import javax.inject.Inject
import javax.persistence.EntityManager

class QuarkusEntityManagerFactory {
    @Inject
    lateinit var entityManager: EntityManager // will be injected by Quarkus (but is not!)
}

class Configuration {
    val mapper = MapperImpl()
    val gateway = JpaGateway(QuarkusEntityManagerFactory().entityManager, mapper)
    val addNote: AddNote = AddNoteImpl(gateway)
    val listNotes: ListNotes = ListNotesImpl(gateway)

    @Produces
    fun getAddNoteUseCase(): AddNote = addNote

    @Produces
    fun getListNotesUseCase(): ListNotes = listNotes
}

dependencies configuration (Gradle + Kotlin DSL):

...
dependencies {
    implementation(project(":app-impl"))
    implementation(project(":infra-gateway-jpa"))
    implementation(project(":infra-rest-jax-rs")) {
        // provided by Quarkus or it's transitive dependencies
        exclude("javax.ws.rs:javax.ws.rs-api:2.0")
        exclude("javax.enterprise:cdi-api:2.0.SP1")
    }

    implementation("io.quarkus:quarkus-kotlin")
    implementation("io.quarkus:quarkus-resteasy-jsonb")
    implementation("io.quarkus:quarkus-jdbc-h2")
    implementation("io.quarkus:quarkus-hibernate-orm")
    implementation(enforcedPlatform("io.quarkus:quarkus-bom:1.0.0.CR1"))
    implementation("io.quarkus:quarkus-resteasy")
}
...

Does it look like correct CDI configuration? Should entityManager be injected by Quarkus before it's entityManager accessed?

aregradle kinquestion

Most helpful comment

@SR-Lut3t1um sure, that might not be the easiest to work with but still a reproducer. Thanks!

Here it is, I hope it's good enough to work with.:
https://gitlab.informatik.uni-bremen.de/tobias5/aut0m4t0n

All 25 comments

Hi,

The CDI configuration is indeed not correct - you need to add @ApplicationScoped to QuarkusEntityManagerFactory and Configuration.

However even after doing that, there seems to be a bug which is most likely another manifestation of https://github.com/quarkusio/quarkus/pull/4905#issuecomment-546851800.

That said the bug specific to gradle, had the application been a Maven application it would have worked. Here is an example of a quickstart that injects the EntityManager.

@geoand Thanks for the reply, any [known] workaround for Gradle bug that you are aware of? BTW. Thanks for a great support!

It's a gradle quarkus plugin issue, not gradle itself.

We are working on having great gradle support, but currently it's incubating :). Stay tuned however, we should have more updates soon.

@maxandersen FYI

thanks for the headsup @geoand !

I've updated the title to reflect the specific issue as I see it and yeah; this is the kind of issue we need to go fix proper in the context of maturing gradle work at #5101

I've jut tried it with maven and also failed to make it working: https://github.com/quarkusio/quarkus/issues/5521

I am taking up this issue. If its fine

Is there any workaround available?

@SR-Lut3t1um currently it is not injected. We are working on improving gradle support on quarkus

@KaenDagger How far are you? Is there any way I can help? What are you working on right now?

Kind regards

@4ntoine have you deleted the branch with the reproducer?

@SR-Lut3t1um was away for exams. Was not able to go ahead. just resumed work

@aloubyansky yes branch is deleted

@KaenDagger do you by chance have a reproducer?

On the way, first test with hibernate-orm + kotlin +gradle. I suppose injection should fail there only as per issue description.
Normal injection works in Kotlin+gradle project

Once you have a reproducer, please add it to this issue. Thanks a lot.

@aloubyansky I have a reproducer. It's a repo of a project I'm working on right now, so it does include more that what's nessisary to reproduce the bug. If you want to I can share it here.

@SR-Lut3t1um sure, that might not be the easiest to work with but still a reproducer. Thanks!

Have you tried 1.3.0.Final, btw?

Have you tried 1.3.0.Final, btw?

Not yet, last version I tried was 1.3.0.CR2
Edit: I did check now, still running into the issue.

@SR-Lut3t1um sure, that might not be the easiest to work with but still a reproducer. Thanks!

Here it is, I hope it's good enough to work with.:
https://gitlab.informatik.uni-bremen.de/tobias5/aut0m4t0n

It's configured to use H2. To reproduce the issue got to: http://127.0.0.1:8080/automaton/test

Is there any new information on this topic?

@SR-Lut3t1um I think you are getting NPE because you are creating AutomatonManager using the constructor instead of injecting it https://gitlab.informatik.uni-bremen.de/tobias5/aut0m4t0n/-/blob/master/src/main/java/org/un1qu3/endpoints/AutomatonSocket.java#L46

About the issue described in the original report, I think the CDI spec expects you to use a getter instead of accessing the field directly here
val gateway = JpaGateway(QuarkusEntityManagerFactory().entityManager, mapper)
Correct, @mkouba ? (see the original description of the issue)

Actually, I also missed the fact that QuarkusEntityManagerFactory is created using constructor too instead of relying on DI to provide a properly initialized instance.
val gateway = JpaGateway(QuarkusEntityManagerFactory().entityManager, mapper)
I am closing this issue since what's described is not expected to work and is not a bug. Thanks everyone.

Was this page helpful?
0 / 5 - 0 ratings