micronaut-sql icon indicating copy to clipboard operation
micronaut-sql copied to clipboard

JPA App working on JVM fails with "org.hibernate.AnnotationException" when running native

Open tmohme opened this issue 3 years ago • 3 comments

Expected Behavior

The application should run native when built with "./gradlew clean dockerBuildNative" the same way as it runs when built with "./gradlew dockerBuild".

Actual Behaviour

When running the native application, I get a org.hibernate.AnnotationException: com.example.DemoClassId has no persistent id property: com.example.DemoClass.id

Steps To Reproduce

clone https://github.com/tmohme/mn-defect-demo, then

  1. run ./gradlew dockerBuild
  2. run 'docker run demo:latest' => should work
  3. run ./gradlew dockerBuildNative
  4. run 'docker run demo:latest' => fails with org.hibernate.AnnotationException: com.example.DemoClassId has no persistent id property: com.example.DemoClass.id

Environment Information

  • macOS 12.5, Intel CPU
  • OpenJDK 64-Bit Server VM Zulu11.56+19-CA (build 11.0.15+10-LTS, mixed mode)

Example Application

https://github.com/tmohme/mn-defect-demo

Version

3.6.0

tmohme avatar Aug 12 '22 15:08 tmohme

libraries like hibernate won't just work out-of-the-box with native image. they need some reflection config.

from the error, it may be enough to simply add @ReflectiveAccess to the bean in question.

yawkat avatar Aug 15 '22 08:08 yawkat

Thank you for your suggestion. I'm aware that Hibernate makes heavy use of reflection. Nevertheless I was under the assumption, that Micronaut already takes care of this . . . because Micronaut has dedicated support for Hibernate and - apart from @EmbeddedId - everything works fine in the native app.

Anyway, adding @ReflectiveAccess to the DemoClassId class was enough to solve the problem for me.

It looks like Micronaut somehow manages this (letting the Annotation survive native-compile) automatically for other JPA Annotations. Maybe it would be worthwhile to extend this support to include @EmbeddedId.

tmohme avatar Aug 15 '22 09:08 tmohme