Skip to content

Commit f1770ee

Browse files
committed
Jakarta Data exception translation for reactive repos (lifecycle methods)
1 parent 86a2402 commit f1770ee

File tree

10 files changed

+119
-112
lines changed

10 files changed

+119
-112
lines changed

tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/multivaluebinding/TopicPostTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
1919
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfMethodInMetamodelFor;
20-
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;
2120
import static org.hibernate.processor.test.util.TestUtil.getMethodFromMetamodelFor;
2221
import static org.junit.jupiter.api.Assertions.assertEquals;
2322
import static org.junit.jupiter.api.Assertions.fail;

tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ else if ( modifiers.contains( Modifier.PROTECTED ) ) {
185185
private static String getFullyQualifiedClassName(Metamodel entity) {
186186
return entity.getElement() instanceof PackageElement packageElement
187187
? packageElement.getQualifiedName().toString() + "." + getGeneratedClassName( entity )
188-
: getGeneratedClassFullyQualifiedName(
189-
(TypeElement) entity.getElement(), entity.getPackageName(), entity.isJakartaDataStyle() );
188+
: getGeneratedClassFullyQualifiedName( (TypeElement) entity.getElement(),
189+
entity.getPackageName(), entity.isJakartaDataStyle() );
190190
}
191191

192192
private static String getGeneratedClassName(Metamodel entity) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractAnnotatedMethod.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,26 @@ void nullCheck(StringBuilder declaration, String paramName) {
8888
.append(paramName)
8989
.append("\");\n");
9090
}
91+
92+
protected void handle(StringBuilder declaration, String handled, String rethrown) {
93+
if ( isReactive() ) {
94+
declaration.append( "\n\t\t\t.onFailure(" )
95+
.append( annotationMetaEntity.importType( handled ) )
96+
.append( ".class)\n" )
97+
.append( "\t\t\t.transform((exception) -> new " )
98+
.append( annotationMetaEntity.importType( rethrown ) )
99+
.append( "(exception.getMessage(), exception))" );
100+
101+
}
102+
else {
103+
declaration
104+
.append( "\tcatch (" )
105+
.append( annotationMetaEntity.importType( handled ) )
106+
.append( " exception) {\n" )
107+
.append( "\t\tthrow new " )
108+
.append( annotationMetaEntity.importType( rethrown ) )
109+
.append( "(exception.getMessage(), exception);\n" )
110+
.append( "\t}\n" );
111+
}
112+
}
91113
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -286,34 +286,28 @@ void handlePageParameters(
286286
}
287287

288288
void convertExceptions(StringBuilder declaration) {
289-
if (dataRepository) {
290-
declaration
291-
.append("\t}\n");
289+
if ( dataRepository ) {
290+
if ( !isReactive() ) {
291+
declaration
292+
.append(";\n")
293+
.append( "\t}\n" );
294+
}
292295
if ( singleResult() ) {
296+
handle( declaration, "jakarta.persistence.NoResultException",
297+
"jakarta.data.exceptions.EmptyResultException" );
298+
handle( declaration, "jakarta.persistence.NonUniqueResultException",
299+
"jakarta.data.exceptions.NonUniqueResultException" );
300+
}
301+
handle( declaration, "jakarta.persistence.PersistenceException",
302+
"jakarta.data.exceptions.DataException" );
303+
if ( isReactive() ) {
293304
declaration
294-
.append("\tcatch (")
295-
.append(annotationMetaEntity.importType("jakarta.persistence.NoResultException"))
296-
.append(" exception) {\n")
297-
.append("\t\tthrow new ")
298-
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
299-
.append("(exception.getMessage(), exception);\n")
300-
.append("\t}\n")
301-
.append("\tcatch (")
302-
.append(annotationMetaEntity.importType("jakarta.persistence.NonUniqueResultException"))
303-
.append(" exception) {\n")
304-
.append("\t\tthrow new ")
305-
.append(annotationMetaEntity.importType("jakarta.data.exceptions.NonUniqueResultException"))
306-
.append("(exception.getMessage(), exception);\n")
307-
.append("\t}\n");
305+
.append( ";\n" );
308306
}
307+
}
308+
else {
309309
declaration
310-
.append("\tcatch (")
311-
.append(annotationMetaEntity.importType("jakarta.persistence.PersistenceException"))
312-
.append(" exception) {\n")
313-
.append("\t\tthrow new ")
314-
.append(annotationMetaEntity.importType("jakarta.data.exceptions.DataException"))
315-
.append("(exception.getMessage(), exception);\n")
316-
.append("\t}\n");
310+
.append(";\n");
317311
}
318312
}
319313

@@ -392,7 +386,7 @@ void makeKeyedPage(StringBuilder declaration, List<String> paramTypes) {
392386
//SHOULD BE new CursoredPageRecord<>
393387
"\t\treturn new CursoredPageRecord(_results.getResultList(), _cursors, _totalResults, pageRequest,\n" +
394388
"\t\t\t\t_results.isLastPage() ? null : afterCursor(_cursors.get(_cursors.size()-1), pageRequest.page()+1, pageRequest.size(), pageRequest.requestTotal()),\n" +
395-
"\t\t\t\t_results.isFirstPage() ? null : beforeCursor(_cursors.get(0), pageRequest.page()-1, pageRequest.size(), pageRequest.requestTotal()));";
389+
"\t\t\t\t_results.isFirstPage() ? null : beforeCursor(_cursors.get(0), pageRequest.page()-1, pageRequest.size(), pageRequest.requestTotal()))";
396390

397391
static final String MAKE_KEYED_PAGE
398392
= "\tvar _unkeyedPage =\n" +
@@ -418,7 +412,7 @@ void tryReturn(StringBuilder declaration, List<String> paramTypes, @Nullable Str
418412
if ( isJakartaCursoredPage(containerType) ) {
419413
makeKeyedPage( declaration, paramTypes );
420414
}
421-
if ( dataRepository ) {
415+
if ( dataRepository && !isReactive() ) {
422416
declaration
423417
.append("\ttry {\n");
424418
}
@@ -430,7 +424,7 @@ void tryReturn(StringBuilder declaration, List<String> paramTypes, @Nullable Str
430424
}
431425
totalResults(declaration, paramTypes);
432426
}
433-
if ( dataRepository ) {
427+
if ( dataRepository && !isReactive() ) {
434428
declaration
435429
.append('\t');
436430
}
@@ -611,11 +605,11 @@ protected void executeSelect(
611605
if ( nullable ) {
612606
unwrapQuery(declaration, unwrapped);
613607
declaration
614-
.append("\t\t\t.getSingleResultOrNull();");
608+
.append("\t\t\t.getSingleResultOrNull()");
615609
}
616610
else {
617611
declaration
618-
.append("\t\t\t.getSingleResult();");
612+
.append("\t\t\t.getSingleResult()");
619613
}
620614
}
621615
else {
@@ -628,28 +622,28 @@ protected void executeSelect(
628622
declaration
629623
.append("\t\t\t.getResultList()\n\t\t\t.toArray(new ")
630624
.append(annotationMetaEntity.importType(returnTypeName))
631-
.append("[0]);");
625+
.append("[0])");
632626
}
633627
break;
634628
case OPTIONAL:
635629
unwrapQuery(declaration, unwrapped);
636630
declaration
637-
.append("\t\t\t.uniqueResultOptional();");
631+
.append("\t\t\t.uniqueResultOptional()");
638632
break;
639633
case STREAM:
640634
declaration
641-
.append("\t\t\t.getResultStream();");
635+
.append("\t\t\t.getResultStream()");
642636
break;
643637
case LIST:
644638
declaration
645-
.append("\t\t\t.getResultList();");
639+
.append("\t\t\t.getResultList()");
646640
break;
647641
case HIB_KEYED_RESULT_LIST:
648642
unwrapQuery(declaration, unwrapped);
649643
declaration
650644
.append("\t\t\t.getKeyedResultList(")
651645
.append(parameterName(HIB_KEYED_PAGE, paramTypes, paramNames))
652-
.append(");");
646+
.append(")");
653647
break;
654648
case JD_PAGE:
655649
if ( isReactive() ) {
@@ -677,8 +671,6 @@ protected void executeSelect(
677671
declaration
678672
.append(')');
679673
}
680-
declaration
681-
.append(';');
682674
break;
683675
case JD_CURSORED_PAGE:
684676
if ( returnTypeName == null ) {
@@ -707,19 +699,17 @@ protected void executeSelect(
707699
declaration
708700
.append("\t\t\t.unwrap(")
709701
.append(annotationMetaEntity.importType(containerType))
710-
.append(".class);");
702+
.append(".class)");
711703

712704
}
713705
else {
714706
final int lastIndex = declaration.length() - 1;
715707
if ( declaration.charAt(lastIndex) == '\n' ) {
716708
declaration.setLength(lastIndex);
717709
}
718-
declaration.append(';');
719710
}
720711
}
721712
}
722-
declaration.append('\n');
723713
}
724714

725715
private static String parameterName(String paramType, List<String> paramTypes, List<String> paramNames) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/CriteriaDeleteMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ String createQueryMethod() {
7777

7878
private void execute(StringBuilder declaration) {
7979
declaration
80-
.append("\t\t\t.executeUpdate();\n");
80+
.append("\t\t\t.executeUpdate()");
8181
}
8282

8383
@Override

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,43 +89,46 @@ private void throwIfNull(StringBuilder declaration) {
8989
.append(')');
9090
}
9191
else if (!nullable) {
92-
declaration
93-
.append(";\n");
94-
if (dataRepository) {
95-
declaration
96-
.append("\t\tif (_result == null) throw new ")
97-
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
98-
.append("(\"No '")
99-
.append(annotationMetaEntity.importType(entity))
100-
.append("' for given id [\" + ")
101-
.append(paramName)
102-
.append(" + \"]\",\n\t\t\t\tnew ")
103-
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
104-
.append("((Object) ")
105-
.append(paramName)
106-
.append(", \"")
107-
.append(entity)
108-
.append("\"));\n")
109-
.append("\t\treturn _result");
92+
if ( isReactive() ) {
93+
//TODO: do something!
11094
}
11195
else {
11296
declaration
113-
.append("\tif (_result == null) throw new ")
114-
.append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException"))
115-
.append("((Object) ")
116-
.append(paramName)
117-
.append(", \"")
118-
.append(entity)
119-
.append("\");\n")
120-
.append("\treturn _result");
97+
.append( ";\n" );
98+
if ( dataRepository ) {
99+
declaration
100+
.append( "\t\tif (_result == null) throw new " )
101+
.append( annotationMetaEntity.importType( "jakarta.data.exceptions.EmptyResultException" ) )
102+
.append( "(\"No '" )
103+
.append( annotationMetaEntity.importType( entity ) )
104+
.append( "' for given id [\" + " )
105+
.append( paramName )
106+
.append( " + \"]\",\n\t\t\t\tnew " )
107+
.append( annotationMetaEntity.importType( "org.hibernate.ObjectNotFoundException" ) )
108+
.append( "((Object) " )
109+
.append( paramName )
110+
.append( ", \"" )
111+
.append( entity )
112+
.append( "\"));\n" )
113+
.append( "\t\treturn _result" );
114+
}
115+
else {
116+
declaration
117+
.append( "\tif (_result == null) throw new " )
118+
.append( annotationMetaEntity.importType( "org.hibernate.ObjectNotFoundException" ) )
119+
.append( "((Object) " )
120+
.append( paramName )
121+
.append( ", \"" )
122+
.append( entity )
123+
.append( "\");\n" )
124+
.append( "\treturn _result" );
125+
}
121126
}
122127
}
123-
declaration
124-
.append(";\n");
125128
}
126129

127130
private void varOrReturn(StringBuilder declaration) {
128-
if (dataRepository) {
131+
if (dataRepository && !isReactive()) {
129132
declaration
130133
.append("\ttry {\n\t");
131134
}
@@ -135,7 +138,7 @@ private void varOrReturn(StringBuilder declaration) {
135138
.append(annotationMetaEntity.staticImport(containerType, "ofNullable"))
136139
.append('(');
137140
}
138-
else if (!nullable) {
141+
else if (!nullable && !isReactive()) {
139142
declaration
140143
.append("\tvar _result = ");
141144
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/LifecycleMethod.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,41 @@ public String getAttributeDeclarationString() {
5757
StringBuilder declaration = new StringBuilder();
5858
preamble(declaration);
5959
nullCheck(declaration, parameterName);
60-
declaration.append("\ttry {\n");
60+
if ( !isReactive() ) {
61+
declaration.append( "\ttry {\n" );
62+
}
6163
delegateCall(declaration);
6264
returnArgument(declaration);
63-
declaration.append("\t}\n");
65+
if ( !isReactive() ) {
66+
if ( returnArgument ) {
67+
declaration
68+
.append( ";\n" );
69+
}
70+
declaration.append( "\t}\n" );
71+
}
72+
convertExceptions( declaration );
73+
if ( isReactive() ) {
74+
declaration
75+
.append( ";\n" );
76+
}
77+
declaration.append("}");
78+
return declaration.toString();
79+
}
80+
81+
private void convertExceptions(StringBuilder declaration) {
6482
if ( operationName.equals("insert") ) {
65-
convertException(declaration,
83+
handle( declaration,
6684
"org.hibernate.exception.ConstraintViolationException",
6785
"jakarta.data.exceptions.EntityExistsException");
6886
}
6987
else {
70-
convertException(declaration,
88+
handle( declaration,
7189
"org.hibernate.StaleStateException",
7290
"jakarta.data.exceptions.OptimisticLockingFailureException");
7391
}
74-
convertException(declaration,
92+
handle( declaration,
7593
"jakarta.persistence.PersistenceException",
7694
"jakarta.data.exceptions.DataException");
77-
declaration.append("}");
78-
return declaration.toString();
7995
}
8096

8197
private void returnArgument(StringBuilder declaration) {
@@ -91,21 +107,13 @@ private void returnArgument(StringBuilder declaration) {
91107
.append("\t\treturn ")
92108
.append(parameterName);
93109
}
94-
declaration
95-
.append(";\n");
96-
}
97-
else {
98-
if ( isReactive() ) {
99-
declaration
100-
.append(";\n");
101-
}
102110
}
103111
}
104112

105113
private void delegateCall(StringBuilder declaration) {
106114
if ( isReactive() ) {
107115
declaration
108-
.append("\t\treturn ")
116+
.append("\treturn ")
109117
.append(sessionName);
110118
if ( isReactiveSessionAccess() ) {
111119
declaration
@@ -188,23 +196,10 @@ private String returnType() {
188196
+ '<' + (returnArgument ? entityType : "Void") + '>';
189197
}
190198
else {
191-
return returnArgument
192-
? entityType
193-
: "void";
199+
return returnArgument ? entityType : "void";
194200
}
195201
}
196202

197-
private void convertException(StringBuilder declaration, String exception, String convertedException) {
198-
declaration
199-
.append("\tcatch (")
200-
.append(annotationMetaEntity.importType(exception))
201-
.append(" exception) {\n")
202-
.append("\t\tthrow new ")
203-
.append(annotationMetaEntity.importType(convertedException))
204-
.append("(exception.getMessage(), exception);\n")
205-
.append("\t}\n");
206-
}
207-
208203
private void notNull(StringBuilder declaration) {
209204
if ( addNonnullAnnotation ) {
210205
declaration

0 commit comments

Comments
 (0)