From ae2f73a3150c34e6a3ca39da84a87d9f8ffa004c Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 14 Jul 2023 17:19:35 +0200 Subject: [PATCH] use @Nonnull annotation in generated named query method --- .../jpamodelgen/annotation/AnnotationMeta.java | 3 ++- .../annotation/NamedQueryMethod.java | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMeta.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMeta.java index a8c9de9863..862514ce58 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMeta.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMeta.java @@ -110,7 +110,8 @@ public abstract class AnnotationMeta implements Metamodel { (SqmSelectStatement) statement, name.substring(1), belongsToDao(), - getSessionType() + getSessionType(), + getContext().addNonnullAnnotation() ) ); } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NamedQueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NamedQueryMethod.java index 0f906751ef..dde6dcb709 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NamedQueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/NamedQueryMethod.java @@ -33,18 +33,21 @@ class NamedQueryMethod implements MetaAttribute { private final String name; private final boolean belongsToDao; private final boolean reactive; + private final boolean addNonnullAnnotation; public NamedQueryMethod( AnnotationMeta annotationMeta, SqmSelectStatement select, String name, boolean belongsToDao, - @Nullable String sessionType) { + @Nullable String sessionType, + boolean addNonnullAnnotation) { this.annotationMeta = annotationMeta; this.select = select; this.name = name; this.belongsToDao = belongsToDao; this.reactive = Constants.MUTINY_SESSION.equals(sessionType); + this.addNonnullAnnotation = addNonnullAnnotation; } @Override @@ -108,9 +111,18 @@ class NamedQueryMethod implements MetaAttribute { } } + void notNull(StringBuilder declaration) { + if ( addNonnullAnnotation ) { + declaration + .append('@') + .append(annotationMeta.importType("jakarta.annotation.Nonnull")) + .append(' '); + } + } + private void comment(StringBuilder declaration) { declaration - .append("\n/**\n * Executes named query {@value #") + .append("\n/**\n * Execute named query {@value #") .append(fieldName()) .append("} defined by annotation of {@link ") .append(annotationMeta.getSimpleName()) @@ -144,6 +156,7 @@ class NamedQueryMethod implements MetaAttribute { declaration .append('('); if ( !belongsToDao ) { + notNull( declaration ); declaration .append(annotationMeta.importType(Constants.ENTITY_MANAGER)) .append(" entityManager");