Skip to content

Commit 681c22b

Browse files
authored
Merge pull request #35129 from Sgitario/35107
Support user methods with @transactional in REST Data with Panache ORM
2 parents c5b7125 + efe7b41 commit 681c22b

File tree

12 files changed

+124
-34
lines changed

12 files changed

+124
-34
lines changed

extensions/panache/hibernate-orm-rest-data-panache/deployment/src/main/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/HibernateOrmPanacheRestProcessor.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ void findEntityResources(CombinedIndexBuildItem index,
8888
ResourceImplementor resourceImplementor = new ResourceImplementor(new EntityClassHelper(index.getComputingIndex()));
8989
ClassOutput classOutput = new GeneratedBeanGizmoAdaptor(implementationsProducer);
9090

91-
for (ClassInfo classInfo : index.getComputingIndex().getKnownDirectImplementors(PANACHE_ENTITY_RESOURCE_INTERFACE)) {
92-
validateResource(index.getComputingIndex(), classInfo);
91+
for (ClassInfo resourceInterface : index.getComputingIndex()
92+
.getKnownDirectImplementors(PANACHE_ENTITY_RESOURCE_INTERFACE)) {
93+
validateResource(index.getComputingIndex(), resourceInterface);
9394

94-
List<Type> generics = getGenericTypes(classInfo);
95-
String resourceInterface = classInfo.name().toString();
95+
List<Type> generics = getGenericTypes(resourceInterface);
9696
String entityType = generics.get(0).name().toString();
9797
String idType = generics.get(1).name().toString();
9898

@@ -120,12 +120,11 @@ void findRepositoryResources(CombinedIndexBuildItem index,
120120
ResourceImplementor resourceImplementor = new ResourceImplementor(new EntityClassHelper(index.getComputingIndex()));
121121
ClassOutput classOutput = new GeneratedBeanGizmoAdaptor(implementationsProducer);
122122

123-
for (ClassInfo classInfo : index.getComputingIndex()
123+
for (ClassInfo resourceInterface : index.getComputingIndex()
124124
.getKnownDirectImplementors(PANACHE_REPOSITORY_RESOURCE_INTERFACE)) {
125-
validateResource(index.getComputingIndex(), classInfo);
125+
validateResource(index.getComputingIndex(), resourceInterface);
126126

127-
List<Type> generics = getGenericTypes(classInfo);
128-
String resourceInterface = classInfo.name().toString();
127+
List<Type> generics = getGenericTypes(resourceInterface);
129128
String repositoryClassName = generics.get(0).name().toString();
130129
String entityType = generics.get(1).name().toString();
131130
String idType = generics.get(2).name().toString();

extensions/panache/hibernate-orm-rest-data-panache/deployment/src/main/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/ResourceImplementor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class ResourceImplementor {
4545
* Implements {@link io.quarkus.rest.data.panache.RestDataResource} interfaces defined in a user application.
4646
* Instances of this class are registered as beans and are later used in the generated JAX-RS controllers.
4747
*/
48-
String implement(ClassOutput classOutput, DataAccessImplementor dataAccessImplementor, String resourceType,
48+
String implement(ClassOutput classOutput, DataAccessImplementor dataAccessImplementor, ClassInfo resourceInterface,
4949
String entityType, List<ClassInfo> resourceMethodListeners) {
50+
String resourceType = resourceInterface.name().toString();
5051
String className = resourceType + "Impl_" + HashUtil.sha1(resourceType);
5152
LOGGER.tracef("Starting generation of '%s'", className);
5253
ClassCreator classCreator = ClassCreator.builder()

extensions/panache/hibernate-orm-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/entity/CollectionsResource.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.util.Collections;
44
import java.util.List;
55

6+
import jakarta.transaction.Transactional;
67
import jakarta.ws.rs.GET;
8+
import jakarta.ws.rs.POST;
79
import jakarta.ws.rs.Path;
810
import jakarta.ws.rs.PathParam;
911

@@ -22,4 +24,15 @@ default Collection findByName(@PathParam("name") String name) {
2224

2325
return collections.get(0);
2426
}
27+
28+
@Transactional
29+
@POST
30+
@Path("/name/{name}")
31+
default Collection addByName(@PathParam("name") String name) {
32+
Collection collection = new Collection();
33+
collection.id = name;
34+
collection.name = name;
35+
Collection.persist(collection);
36+
return collection;
37+
}
2538
}

extensions/panache/hibernate-orm-rest-data-panache/deployment/src/test/java/io/quarkus/hibernate/orm/rest/data/panache/deployment/entity/PanacheEntityResourcePostMethodTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.quarkus.hibernate.orm.rest.data.panache.deployment.entity;
22

3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import org.junit.jupiter.api.Test;
37
import org.junit.jupiter.api.extension.RegisterExtension;
48

59
import io.quarkus.hibernate.orm.rest.data.panache.deployment.AbstractPostMethodTest;
@@ -14,4 +18,13 @@ class PanacheEntityResourcePostMethodTest extends AbstractPostMethodTest {
1418
Item.class, ItemsResource.class)
1519
.addAsResource("application.properties")
1620
.addAsResource("import.sql"));
21+
22+
@Test
23+
void shouldCopyUserMethodsAnnotatedWithTransactional() {
24+
given().accept("application/json")
25+
.when().post("/collections/name/mycollection")
26+
.then().statusCode(200)
27+
.and().body("id", is("mycollection"))
28+
.and().body("name", is("mycollection"));
29+
}
1730
}

extensions/panache/hibernate-reactive-rest-data-panache/deployment/src/main/java/io/quarkus/hibernate/reactive/rest/data/panache/deployment/HibernateReactivePanacheRestProcessor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ void findEntityResources(CombinedIndexBuildItem indexBuildItem,
7373
ResourceImplementor resourceImplementor = new ResourceImplementor(new EntityClassHelper(index));
7474
ClassOutput classOutput = new GeneratedBeanGizmoAdaptor(implementationsProducer);
7575

76-
for (ClassInfo classInfo : index.getKnownDirectImplementors(PANACHE_ENTITY_RESOURCE_INTERFACE)) {
77-
validateResource(index, classInfo);
76+
for (ClassInfo resourceInterface : index.getKnownDirectImplementors(PANACHE_ENTITY_RESOURCE_INTERFACE)) {
77+
validateResource(index, resourceInterface);
7878

79-
List<Type> generics = getGenericTypes(classInfo);
80-
String resourceInterface = classInfo.name().toString();
79+
List<Type> generics = getGenericTypes(resourceInterface);
8180
String entityType = generics.get(0).name().toString();
8281
String idType = generics.get(1).name().toString();
8382

@@ -105,11 +104,10 @@ void findRepositoryResources(CombinedIndexBuildItem indexBuildItem,
105104
ResourceImplementor resourceImplementor = new ResourceImplementor(new EntityClassHelper(index));
106105
ClassOutput classOutput = new GeneratedBeanGizmoAdaptor(implementationsProducer);
107106

108-
for (ClassInfo classInfo : index.getKnownDirectImplementors(PANACHE_REPOSITORY_RESOURCE_INTERFACE)) {
109-
validateResource(index, classInfo);
107+
for (ClassInfo resourceInterface : index.getKnownDirectImplementors(PANACHE_REPOSITORY_RESOURCE_INTERFACE)) {
108+
validateResource(index, resourceInterface);
110109

111-
List<Type> generics = getGenericTypes(classInfo);
112-
String resourceInterface = classInfo.name().toString();
110+
List<Type> generics = getGenericTypes(resourceInterface);
113111
String repositoryClassName = generics.get(0).name().toString();
114112
String entityType = generics.get(1).name().toString();
115113
String idType = generics.get(2).name().toString();

extensions/panache/hibernate-reactive-rest-data-panache/deployment/src/main/java/io/quarkus/hibernate/reactive/rest/data/panache/deployment/ResourceImplementor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class ResourceImplementor {
4747
* Implements {@link io.quarkus.rest.data.panache.ReactiveRestDataResource} interfaces defined in a user application.
4848
* Instances of this class are registered as beans and are later used in the generated JAX-RS controllers.
4949
*/
50-
String implement(ClassOutput classOutput, DataAccessImplementor dataAccessImplementor, String resourceType,
50+
String implement(ClassOutput classOutput, DataAccessImplementor dataAccessImplementor, ClassInfo resourceInterface,
5151
String entityType, List<ClassInfo> resourceMethodListeners) {
52+
String resourceType = resourceInterface.name().toString();
5253
String className = resourceType + "Impl_" + HashUtil.sha1(resourceType);
5354
LOGGER.tracef("Starting generation of '%s'", className);
5455
ClassCreator classCreator = ClassCreator.builder()

extensions/panache/mongodb-rest-data-panache/deployment/src/main/java/io/quarkus/mongodb/rest/data/panache/deployment/MongoPanacheRestProcessor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ void findEntityResources(CombinedIndexBuildItem index, Capabilities capabilities
7676
ResourceImplementor resourceImplementor = new ResourceImplementor(entityClassHelper);
7777
ClassOutput classOutput = new GeneratedBeanGizmoAdaptor(implementationsProducer);
7878

79-
for (ClassInfo classInfo : index.getComputingIndex()
79+
for (ClassInfo resourceInterface : index.getComputingIndex()
8080
.getKnownDirectImplementors(PANACHE_MONGO_ENTITY_RESOURCE_INTERFACE)) {
81-
validateResource(index.getComputingIndex(), classInfo);
81+
validateResource(index.getComputingIndex(), resourceInterface);
8282

83-
List<Type> generics = getGenericTypes(classInfo);
84-
String resourceInterface = classInfo.name().toString();
83+
List<Type> generics = getGenericTypes(resourceInterface);
8584
String entityType = generics.get(0).toString();
8685
String idType = generics.get(1).toString();
8786

@@ -109,12 +108,11 @@ void findRepositoryResources(CombinedIndexBuildItem index, Capabilities capabili
109108
ResourceImplementor resourceImplementor = new ResourceImplementor(entityClassHelper);
110109
ClassOutput classOutput = new GeneratedBeanGizmoAdaptor(implementationsProducer);
111110

112-
for (ClassInfo classInfo : index.getComputingIndex()
111+
for (ClassInfo resourceInterface : index.getComputingIndex()
113112
.getKnownDirectImplementors(PANACHE_MONGO_REPOSITORY_RESOURCE_INTERFACE)) {
114-
validateResource(index.getComputingIndex(), classInfo);
113+
validateResource(index.getComputingIndex(), resourceInterface);
115114

116-
List<Type> generics = getGenericTypes(classInfo);
117-
String resourceInterface = classInfo.name().toString();
115+
List<Type> generics = getGenericTypes(resourceInterface);
118116
String repositoryClassName = generics.get(0).toString();
119117
String entityType = generics.get(1).toString();
120118
String idType = generics.get(2).toString();

extensions/panache/mongodb-rest-data-panache/deployment/src/main/java/io/quarkus/mongodb/rest/data/panache/deployment/ResourceImplementor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import jakarta.enterprise.context.ApplicationScoped;
99

10+
import org.jboss.jandex.ClassInfo;
1011
import org.jboss.jandex.FieldInfo;
1112
import org.jboss.logging.Logger;
1213

@@ -37,8 +38,9 @@ class ResourceImplementor {
3738
this.entityClassHelper = entityClassHelper;
3839
}
3940

40-
String implement(ClassOutput classOutput, DataAccessImplementor dataAccessImplementor, String resourceType,
41+
String implement(ClassOutput classOutput, DataAccessImplementor dataAccessImplementor, ClassInfo resourceInterface,
4142
String entityType) {
43+
String resourceType = resourceInterface.name().toString();
4244
String className = resourceType + "Impl_" + HashUtil.sha1(resourceType);
4345
LOGGER.tracef("Starting generation of '%s'", className);
4446
ClassCreator classCreator = ClassCreator.builder()

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/JaxRsResourceImplementor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.quarkus.rest.data.panache.deployment.methods.ListMethodImplementor;
2727
import io.quarkus.rest.data.panache.deployment.methods.MethodImplementor;
2828
import io.quarkus.rest.data.panache.deployment.methods.UpdateMethodImplementor;
29+
import io.quarkus.rest.data.panache.deployment.methods.UserMethodsWithTransactionalImplementor;
2930
import io.quarkus.rest.data.panache.deployment.methods.hal.ListHalMethodImplementor;
3031
import io.quarkus.rest.data.panache.deployment.properties.ResourceProperties;
3132
import io.quarkus.runtime.util.HashUtil;
@@ -48,6 +49,7 @@ class JaxRsResourceImplementor {
4849
new AddMethodImplementor(capabilities),
4950
new UpdateMethodImplementor(capabilities),
5051
new DeleteMethodImplementor(capabilities),
52+
new UserMethodsWithTransactionalImplementor(capabilities),
5153
// The list hal endpoint needs to be added for both resteasy classic and resteasy reactive
5254
// because the pagination links are programmatically added.
5355
new ListHalMethodImplementor(capabilities));
@@ -77,7 +79,7 @@ void implement(ClassOutput classOutput, ResourceMetadata resourceMetadata, Resou
7779
.classOutput(classOutput).className(controllerClassName);
7880

7981
if (resourceMetadata.getResourceInterface() != null) {
80-
classCreatorBuilder.interfaces(resourceMetadata.getResourceInterface());
82+
classCreatorBuilder.interfaces(resourceMetadata.getResourceInterface().name().toString());
8183
}
8284

8385
ClassCreator classCreator = classCreatorBuilder.build();

extensions/panache/rest-data-panache/deployment/src/main/java/io/quarkus/rest/data/panache/deployment/ResourceMetadata.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import org.jboss.jandex.ClassInfo;
56
import org.jboss.jandex.Type;
67

78
public class ResourceMetadata {
@@ -19,7 +20,7 @@ public class ResourceMetadata {
1920
/**
2021
* Application interface that extends RestDataResource interface.
2122
*/
22-
private final String resourceInterface;
23+
private final ClassInfo resourceInterface;
2324

2425
/**
2526
* Entity class that is used by the resource.
@@ -36,12 +37,12 @@ public class ResourceMetadata {
3637
*/
3738
private final Map<String, Type> fields;
3839

39-
public ResourceMetadata(String resourceClass, String resourceInterface, String entityType, String idType,
40+
public ResourceMetadata(String resourceClass, ClassInfo resourceInterface, String entityType, String idType,
4041
Map<String, Type> fields) {
41-
this(resourceClass, resourceInterface, resourceInterface, entityType, idType, fields);
42+
this(resourceClass, resourceInterface.name().toString(), resourceInterface, entityType, idType, fields);
4243
}
4344

44-
public ResourceMetadata(String resourceClass, String resourceName, String resourceInterface, String entityType,
45+
public ResourceMetadata(String resourceClass, String resourceName, ClassInfo resourceInterface, String entityType,
4546
String idType, Map<String, Type> fields) {
4647
this.resourceClass = resourceClass;
4748
this.resourceName = resourceName;
@@ -59,7 +60,7 @@ public String getResourceName() {
5960
return resourceName;
6061
}
6162

62-
public String getResourceInterface() {
63+
public ClassInfo getResourceInterface() {
6364
return resourceInterface;
6465
}
6566

0 commit comments

Comments
 (0)