quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.73k stars 2.67k forks source link

JPA/Hibernate issues with Kotlin started with Quarkus 3.7.0 #39772

Closed sven-geisenhainer closed 6 months ago

sven-geisenhainer commented 6 months ago

Describe the bug

With Quarkus 3.7.0 a serious JPA/Hibernate malfunction was introduced when using Kotlin which cannot be observed with 3.6.9, and which also cannot be reproduced using Java code.

The bug has several different symptoms - one is that lazy loading of entity fields does not work anymore - Hibernate will not fetch the fields marked as being lazy at all, even if they get accessed in the code - see the reproducer below.

The bug still exists in current stable release 3.9.1.

Expected behavior

Entity fields marked as being lazy by @Basic(fetch = FetchType.LAZY) will get loaded when accessed in the code. With Hibernates SQL logging feature enabled, a log message like this will appear on field access:

Hibernate: 
    select
        t1_0.data 
    from
        test t1_0 
    where
        t1_0.id=?

Actual behavior

Fields marked as being lazy will not get fetched at all, and the above message is not logged.

How to Reproduce?

Use the attached project (requires local Docker daemon for PSQL devservices) and run the contained test (mvnw test), it will fail with: org.opentest4j.AssertionFailedError: expected: not <null>

kotlin-test.zip

Output of uname -a or ver

Darwin MacBook-Pro.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64

Output of java -version

openjdk version "21.0.1" 2023-10-17 LTS OpenJDK Runtime Environment Zulu21.30+15-CA (build 21.0.1+12-LTS) OpenJDK 64-Bit Server VM Zulu21.30+15-CA (build 21.0.1+12-LTS, mixed mode, sharing)

Quarkus version or git rev

3.9.1

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)

Additional information

No response

quarkus-bot[bot] commented 6 months ago

/cc @geoand (kotlin), @gsmet (hibernate-orm), @yrodiere (hibernate-orm)

yrodiere commented 6 months ago

Thanks for reporting.

Can this be reproduced without Quarkus, e.g. with https://github.com/hibernate/hibernate-test-case-templates/blob/main/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java (or a Kotlin equivalent)?

If so, it would be great to take that reproducer directly to the Hibernate ORM Jira: https://hibernate.atlassian.net/

If not, then there's a chance we can do something about it in Quarkus itself.

sven-geisenhainer commented 6 months ago

Yes I can reproduce this using a Kotlin specific Hibernate ORM Unit Test without Quarkus, so obviously this is a Hibernate issue - please forward the issue to the appropriate place - Thanks.

Hibernate reproducer: hibernate-test.zip

mbladel commented 6 months ago

Hi @sven-geisenhainer, it would be great if you could open a new issue in Hibernate's tracker attaching your reproducer. Please ensure you're enabling bytecode enhancement (which is on by default in Quarkus) as it's needed to support lazy attribute loading.

sven-geisenhainer commented 6 months ago

Bytecode enhancement was indeed not enabled in my reproducer, but enabling it did not help. I created a ticket in the Hibernate tracker.

sven-geisenhainer commented 6 months ago

Can be closed, issue is caused by using val instead of var when defining entity properties which worked in the past.

geoand commented 6 months ago

Thanks for the update