better handling of the session variable in repositories

This commit is contained in:
Gavin King 2024-02-06 12:33:10 +01:00
parent da04eb7736
commit 6fef20cd49
4 changed files with 60 additions and 14 deletions

View File

@ -112,6 +112,7 @@ public abstract class AnnotationMeta implements Metamodel {
name.substring(1),
belongsToDao(),
getSessionType(),
getSessionVariableName(),
getContext().addNonnullAnnotation()
)
);
@ -162,6 +163,10 @@ public abstract class AnnotationMeta implements Metamodel {
});
}
protected String getSessionVariableName() {
return "entityManager";
}
abstract boolean belongsToDao();
abstract @Nullable String getSessionType();

View File

@ -58,6 +58,9 @@ import static javax.lang.model.util.ElementFilter.methodsIn;
import static org.hibernate.internal.util.StringHelper.qualify;
import static org.hibernate.jpamodelgen.annotation.QueryMethod.isOrderParam;
import static org.hibernate.jpamodelgen.annotation.QueryMethod.isPageParam;
import static org.hibernate.jpamodelgen.util.Constants.HIB_SESSION;
import static org.hibernate.jpamodelgen.util.Constants.HIB_STATELESS_SESSION;
import static org.hibernate.jpamodelgen.util.Constants.MUTINY_SESSION;
import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES;
import static org.hibernate.jpamodelgen.util.NullnessUtil.castNonNull;
import static org.hibernate.jpamodelgen.util.TypeUtils.containsAnnotation;
@ -342,6 +345,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
typeName,
name,
sessionType,
getSessionVariableName(sessionType),
context.addInjectAnnotation(),
context.addNonnullAnnotation()
)
@ -429,7 +433,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|| determineAnnotationSpecifiedAccessType( memberOfClass ) != null )
&& !containsAnnotation( memberOfClass, Constants.TRANSIENT )
&& !memberOfClass.getModifiers().contains( Modifier.TRANSIENT )
&& !memberOfClass.getModifiers().contains( Modifier.STATIC );
&& !memberOfClass.getModifiers().contains( Modifier.STATIC )
&& !( memberOfClass.getKind() == ElementKind.METHOD
&& isSessionGetter( (ExecutableElement) memberOfClass ) );
}
private void addQueryMethods(List<ExecutableElement> queryMethods) {
@ -621,7 +627,23 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return new String[] { type, name };
}
}
return new String[] { sessionType, "entityManager" };
return new String[] { getSessionType(), getSessionVariableName() };
}
@Override
protected String getSessionVariableName() {
return getSessionVariableName(sessionType);
}
private static String getSessionVariableName(String sessionType) {
switch (sessionType) {
case HIB_SESSION:
case HIB_STATELESS_SESSION:
case MUTINY_SESSION:
return "session";
default:
return "entityManager";
}
}
private static List<String> enabledFetchProfiles(ExecutableElement method) {

View File

@ -17,7 +17,8 @@ public class DaoConstructor implements MetaAttribute {
private final Metamodel annotationMetaEntity;
private final String constructorName;
private final String methodName;
private final String returnTypeName;
private final String sessionTypeName;
private final String sessionVariableName;
private final boolean addInjectAnnotation;
private final boolean addNonnullAnnotation;
@ -25,13 +26,15 @@ public class DaoConstructor implements MetaAttribute {
Metamodel annotationMetaEntity,
String constructorName,
String methodName,
String returnTypeName,
String sessionTypeName,
String sessionVariableName,
boolean addInjectAnnotation,
boolean addNonnullAnnotation) {
this.annotationMetaEntity = annotationMetaEntity;
this.constructorName = constructorName;
this.methodName = methodName;
this.returnTypeName = returnTypeName;
this.sessionTypeName = sessionTypeName;
this.sessionVariableName = sessionVariableName;
this.addInjectAnnotation = addInjectAnnotation;
this.addNonnullAnnotation = addNonnullAnnotation;
}
@ -53,8 +56,10 @@ public class DaoConstructor implements MetaAttribute {
.append("\nprivate final ");
notNull( declaration );
declaration
.append(annotationMetaEntity.importType(returnTypeName))
.append(" entityManager;")
.append(annotationMetaEntity.importType(sessionTypeName))
.append(" ")
.append(sessionVariableName)
.append(";")
.append("\n");
inject( declaration );
declaration
@ -63,19 +68,27 @@ public class DaoConstructor implements MetaAttribute {
.append("(");
notNull( declaration );
declaration
.append(annotationMetaEntity.importType(returnTypeName))
.append(" entityManager) {")
.append("\n\tthis.entityManager = entityManager;")
.append(annotationMetaEntity.importType(sessionTypeName))
.append(" ")
.append(sessionVariableName)
.append(") {")
.append("\n\tthis.")
.append(sessionVariableName)
.append(" = ")
.append(sessionVariableName)
.append(";")
.append("\n}")
.append("\n\n")
.append("public ");
notNull( declaration );
declaration
.append(annotationMetaEntity.importType(returnTypeName))
.append(annotationMetaEntity.importType(sessionTypeName))
.append(" ")
.append(methodName)
.append("() {")
.append("\n\treturn entityManager;")
.append("\n\treturn ")
.append(sessionVariableName)
.append(";")
.append("\n}");
return declaration.toString();
}

View File

@ -33,6 +33,7 @@ class NamedQueryMethod implements MetaAttribute {
private final String name;
private final boolean belongsToDao;
private final boolean reactive;
private final String sessionVariableName;
private final boolean addNonnullAnnotation;
public NamedQueryMethod(
@ -41,12 +42,14 @@ class NamedQueryMethod implements MetaAttribute {
String name,
boolean belongsToDao,
@Nullable String sessionType,
String sessionVariableName,
boolean addNonnullAnnotation) {
this.annotationMeta = annotationMeta;
this.select = select;
this.name = name;
this.belongsToDao = belongsToDao;
this.reactive = Constants.MUTINY_SESSION.equals(sessionType);
this.sessionVariableName = sessionVariableName;
this.addNonnullAnnotation = addNonnullAnnotation;
}
@ -71,7 +74,9 @@ class NamedQueryMethod implements MetaAttribute {
parameters( sortedParameters, declaration );
declaration
.append(" {")
.append("\n\treturn entityManager.createNamedQuery(")
.append("\n\treturn ")
.append(sessionVariableName)
.append(".createNamedQuery(")
.append(fieldName())
.append(")");
for ( SqmParameter<?> param : sortedParameters ) {
@ -159,7 +164,8 @@ class NamedQueryMethod implements MetaAttribute {
notNull( declaration );
declaration
.append(annotationMeta.importType(Constants.ENTITY_MANAGER))
.append(" entityManager");
.append(" ")
.append(sessionVariableName);
}
int i = 0;
for ( SqmParameter<?> param : sortedParameters) {