use @Nonnull annotation in generated named query method

This commit is contained in:
Gavin King 2023-07-14 17:19:35 +02:00
parent ec830945e6
commit ae2f73a315
2 changed files with 17 additions and 3 deletions

View File

@ -110,7 +110,8 @@ public abstract class AnnotationMeta implements Metamodel {
(SqmSelectStatement<?>) statement,
name.substring(1),
belongsToDao(),
getSessionType()
getSessionType(),
getContext().addNonnullAnnotation()
)
);
}

View File

@ -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");