forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support user methods with @transactional in REST Data with Panache ORM
Since the annotation `@Transactional` is ignored when used in default methods, we need to copy the default method from the interface into the generated resource. After these changes, we can now use the `@Transacational` annotation. Note that this solution is based on what REST Client also does. Fix quarkusio#35107
- Loading branch information
Showing
12 changed files
with
124 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...quarkus/rest/data/panache/deployment/methods/UserMethodsWithTransactionalImplementor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.quarkus.rest.data.panache.deployment.methods; | ||
|
||
import org.jboss.jandex.AnnotationTarget; | ||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.deployment.Capabilities; | ||
import io.quarkus.deployment.Capability; | ||
import io.quarkus.gizmo.ClassCreator; | ||
import io.quarkus.gizmo.FieldDescriptor; | ||
import io.quarkus.gizmo.MethodCreator; | ||
import io.quarkus.gizmo.MethodDescriptor; | ||
import io.quarkus.gizmo.ResultHandle; | ||
import io.quarkus.rest.data.panache.deployment.ResourceMetadata; | ||
import io.quarkus.rest.data.panache.deployment.properties.ResourceProperties; | ||
|
||
/** | ||
* Propagate the user methods annotated with `@Transactional`. | ||
* This implementor is only used if Hibernate ORM is present. | ||
*/ | ||
public final class UserMethodsWithTransactionalImplementor implements MethodImplementor { | ||
|
||
public static final DotName TRANSACTIONAL = DotName.createSimple("jakarta.transaction.Transactional"); | ||
|
||
private final Capabilities capabilities; | ||
|
||
public UserMethodsWithTransactionalImplementor(Capabilities capabilities) { | ||
this.capabilities = capabilities; | ||
} | ||
|
||
@Override | ||
public void implement(ClassCreator classCreator, ResourceMetadata resourceMetadata, | ||
ResourceProperties resourceProperties, FieldDescriptor resourceField) { | ||
if (capabilities.isPresent(Capability.HIBERNATE_ORM) && resourceMetadata.getResourceInterface() != null) { | ||
for (var methodInfo : resourceMetadata.getResourceInterface().methods()) { | ||
// we only need to propagate the user methods annotated with `@Transactional` | ||
if (methodInfo.hasAnnotation(TRANSACTIONAL)) { | ||
MethodCreator methodCreator = classCreator.getMethodCreator(MethodDescriptor.of(methodInfo)); | ||
methodCreator.setSignature(methodInfo.genericSignatureIfRequired()); | ||
for (var annotation : methodInfo.annotations()) { | ||
if (annotation.target().kind() == AnnotationTarget.Kind.METHOD_PARAMETER) { | ||
short position = annotation.target().asMethodParameter().position(); | ||
methodCreator.getParameterAnnotations(position).addAnnotation(annotation); | ||
} | ||
|
||
if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) { | ||
methodCreator.addAnnotation(annotation); | ||
} | ||
} | ||
|
||
ResultHandle[] params = new ResultHandle[methodInfo.parametersCount()]; | ||
for (int paramIdx = 0; paramIdx < methodInfo.parametersCount(); paramIdx++) { | ||
params[paramIdx] = methodCreator.getMethodParam(paramIdx); | ||
} | ||
|
||
methodCreator.returnValue( | ||
methodCreator.invokeSpecialInterfaceMethod(methodInfo, methodCreator.getThis(), params)); | ||
methodCreator.close(); | ||
} | ||
} | ||
} | ||
} | ||
} |