quarkusio / quarkus

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

Spring-Data-JPA Extension lacks on hibernate feature ragarding @EmbeddedId and @IdClass annotations #34680

Open wi21165 opened 1 year ago

wi21165 commented 1 year ago

Description

Hello everyone,

I came across an unsupported feature in the "quarkus-spring-data-jpa" extension which is often very usefull or quite mandatory and therefore an addition to the already fixed bug at: https://github.com/quarkusio/quarkus/issues/10870

The problem is regarding on how one can implement composit primary keys in hibernate orm. As the documentations of Hibernate shows (here) there are two ways to implement composite primary keys in hibernate:

There are of course many differenced between those two ways but the one I want to highlight is that it is only possible to use the @GeneratedValue annotation on the compositekey class when using the @IdClass variant (see).

So whats the problem with the quarkus extension? In quarkus only the @EmbeddedId annotation is supported for composite primary keys. Therefore it's impossible to use eg. the @GeneratedValue annotation on a part of a composite primary key. If you dare to try it this fancy error occurs:

2023-06-12 13:53:15,117 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.spring.data.deployment.SpringDataJPAProcessor#build threw an exception: java.lang.IllegalArgumentException: Currently the @Id or @EmbeddedId annotation can only be placed on a single field or method. Offending class is my.hibernate.Class
    at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.getIdAnnotationTargetRec(StockMethodsAdder.java:1063)
    at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.getIdAnnotationTarget(StockMethodsAdder.java:1039)
    at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.generateSave(StockMethodsAdder.java:149)
    at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.add(StockMethodsAdder.java:89)
    at io.quarkus.spring.data.deployment.generate.SpringDataRepositoryCreator.implementCrudRepository(SpringDataRepositoryCreator.java:115)
    at io.quarkus.spring.data.deployment.SpringDataJPAProcessor.implementCrudRepositories(SpringDataJPAProcessor.java:264)
    at io.quarkus.spring.data.deployment.SpringDataJPAProcessor.build(SpringDataJPAProcessor.java:129)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.quarkus.deployment.ExtensionLoader$3.execute(ExtensionLoader.java:909)
    at io.quarkus.builder.BuildContext.run(BuildContext.java:282)
    at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads.JBossThread.run(JBossThread.java:501)

I think there are quite a lot users who would benefit from this feature as there are some related questions online eg.https://stackoverflow.com/questions/61211391/are-composite-primary-keys-in-jpa-with-quarkus-possible

Let me know if anything is unclear.. Thanks

Implementation ideas

No response

quarkus-bot[bot] commented 1 year ago

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

geoand commented 1 year ago

Based on the numbers from the StackOverflow question, this does indeed seem to be a popular request.

I'll keep it mind for some upcoming Quarkus version.

yrodiere commented 1 year ago

In quarkus only the @EmbeddedId annotation is supported for composite primary keys. Therefore it's impossible to use eg. the @GeneratedValue annotation on a part of a composite primary key.

To clarify, Quarkus works just fine with @IdClass, as long as you use Jakarta/Quarkus/Panache APIs. Only the Quarkus extensions that implement Spring Data APIs do not handle @IdClass.

(Just trying to make things clear for others ending up here, I know this does not address your problem)

aureamunoz commented 2 weeks ago

cc @aureamunoz