better handling of the session variable in repositories

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

View File

@ -112,6 +112,7 @@ public abstract class AnnotationMeta implements Metamodel {
name.substring(1), name.substring(1),
belongsToDao(), belongsToDao(),
getSessionType(), getSessionType(),
getSessionVariableName(),
getContext().addNonnullAnnotation() getContext().addNonnullAnnotation()
) )
); );
@ -162,6 +163,10 @@ public abstract class AnnotationMeta implements Metamodel {
}); });
} }
protected String getSessionVariableName() {
return "entityManager";
}
abstract boolean belongsToDao(); abstract boolean belongsToDao();
abstract @Nullable String getSessionType(); 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.internal.util.StringHelper.qualify;
import static org.hibernate.jpamodelgen.annotation.QueryMethod.isOrderParam; import static org.hibernate.jpamodelgen.annotation.QueryMethod.isOrderParam;
import static org.hibernate.jpamodelgen.annotation.QueryMethod.isPageParam; 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.Constants.SESSION_TYPES;
import static org.hibernate.jpamodelgen.util.NullnessUtil.castNonNull; import static org.hibernate.jpamodelgen.util.NullnessUtil.castNonNull;
import static org.hibernate.jpamodelgen.util.TypeUtils.containsAnnotation; import static org.hibernate.jpamodelgen.util.TypeUtils.containsAnnotation;
@ -342,6 +345,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
typeName, typeName,
name, name,
sessionType, sessionType,
getSessionVariableName(sessionType),
context.addInjectAnnotation(), context.addInjectAnnotation(),
context.addNonnullAnnotation() context.addNonnullAnnotation()
) )
@ -429,7 +433,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|| determineAnnotationSpecifiedAccessType( memberOfClass ) != null ) || determineAnnotationSpecifiedAccessType( memberOfClass ) != null )
&& !containsAnnotation( memberOfClass, Constants.TRANSIENT ) && !containsAnnotation( memberOfClass, Constants.TRANSIENT )
&& !memberOfClass.getModifiers().contains( Modifier.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) { private void addQueryMethods(List<ExecutableElement> queryMethods) {
@ -621,7 +627,23 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return new String[] { type, name }; 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) { private static List<String> enabledFetchProfiles(ExecutableElement method) {

View File

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

View File

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