HHH-17772 support for returning the argument from lifecycle methods

This commit is contained in:
Gavin King 2024-03-01 21:00:07 +01:00
parent ca12a4c874
commit 2beb85e695
3 changed files with 35 additions and 6 deletions

View File

@ -140,4 +140,10 @@ public interface BookAuthorRepository {
@Find
List<Book> allBooksWithLotsOfSorting(Sort<? super Book> s1, Order<? super Book> order, Sort<? super Book>... s3);
@Save
Book write(Book book);
@Insert
Iterable<Book> createAll(Iterable<Book> books);
}

View File

@ -824,12 +824,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
Diagnostic.Kind.ERROR );
}
else if ( returnType == null || returnType.getKind() != TypeKind.VOID ) {
else if ( returnType == null ) {
context.message( method,
"must be declared 'void'",
Diagnostic.Kind.ERROR );
}
else {
final boolean returnArgument = returnType.getKind() != TypeKind.VOID;
final String operation = lifecycleOperation( method );
final VariableElement parameter = method.getParameters().get(0);
final TypeMirror parameterType = parameter.asType();
@ -844,6 +845,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
"incorrect parameter type '" + parameterType + "' is not annotated '@Entity'",
Diagnostic.Kind.ERROR );
}
else if ( returnArgument
&& !context.getTypeUtils().isSameType( returnType, parameterType ) ) {
context.message( parameter,
"return type '" + returnType
+ "' disagrees with parameter type '" + parameterType + "'",
Diagnostic.Kind.ERROR );
}
else {
putMember(
method.getSimpleName().toString()
@ -856,7 +864,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
getSessionVariableName(),
operation,
context.addNonnullAnnotation(),
declaredType != parameterType
declaredType != parameterType,
returnArgument
)
);
}

View File

@ -18,6 +18,7 @@ public class LifecycleMethod implements MetaAttribute {
private final String operationName;
private final boolean addNonnullAnnotation;
private final boolean iterateParameter;
private final boolean returnArgument;
public LifecycleMethod(
AnnotationMetaEntity annotationMetaEntity,
@ -27,7 +28,8 @@ public class LifecycleMethod implements MetaAttribute {
String sessionName,
String operationName,
boolean addNonnullAnnotation,
boolean iterateParameter) {
boolean iterateParameter,
boolean returnArgument) {
this.annotationMetaEntity = annotationMetaEntity;
this.entity = entity;
this.methodName = methodName;
@ -36,6 +38,7 @@ public class LifecycleMethod implements MetaAttribute {
this.operationName = operationName;
this.addNonnullAnnotation = addNonnullAnnotation;
this.iterateParameter = iterateParameter;
this.returnArgument = returnArgument;
}
@Override
@ -55,6 +58,8 @@ public class LifecycleMethod implements MetaAttribute {
nullCheck(declaration);
declaration.append("\ttry {\n");
delegateCall(declaration);
returnArgument(declaration);
declaration.append("\t}\n");
if ( operationName.equals("insert") ) {
convertException(declaration,
"jakarta.persistence.EntityExistsException",
@ -72,6 +77,15 @@ public class LifecycleMethod implements MetaAttribute {
return declaration.toString();
}
private void returnArgument(StringBuilder declaration) {
if ( returnArgument ) {
declaration
.append("\t\treturn ")
.append(parameterName)
.append(";\n");
}
}
private void delegateCall(StringBuilder declaration) {
if ( iterateParameter ) {
declaration
@ -92,13 +106,13 @@ public class LifecycleMethod implements MetaAttribute {
declaration
.append("\t\t}\n");
}
declaration
.append("\t}\n");
}
private void preamble(StringBuilder declaration) {
declaration
.append("\n@Override\npublic void ")
.append("\n@Override\npublic ")
.append(returnArgument ? annotationMetaEntity.importType(entity) : "void")
.append(' ')
.append(methodName)
.append('(');
notNull(declaration);