Support sessions of type Uni<Mutiny.Session>

This commit is contained in:
Stéphane Épardaud 2023-12-05 11:58:50 +01:00 committed by Gavin King
parent 3ca494be79
commit 36c9ce9d20
5 changed files with 34 additions and 6 deletions

View File

@ -183,7 +183,29 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
}
boolean isReactive() {
return MUTINY_SESSION.equals(sessionType);
return MUTINY_SESSION.equals(sessionType)
|| UNI_MUTINY_SESSION.equals(sessionType);
}
boolean isReactiveSession() {
return UNI_MUTINY_SESSION.equals(sessionType);
}
String localSessionName() {
return isReactiveSession() ? "resolvedSession" : sessionName;
}
void chainSession(StringBuilder declaration) {
// Reactive calls always have a return type
if ( isReactiveSession() ) {
declaration.append("\treturn ").append(sessionName).append(".chain(").append(localSessionName()).append(" -> {\n\t");
}
}
void chainSessionEnd(boolean isUpdate, StringBuilder declaration) {
if ( isReactiveSession() ) {
declaration.append("\n\t});");
}
}
void setPage(StringBuilder declaration, String paramName, String paramType) {

View File

@ -459,7 +459,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( method.getParameters().isEmpty() ) {
final TypeMirror type = method.getReturnType();
if ( type.getKind() == TypeKind.DECLARED ) {
final DeclaredType declaredType = (DeclaredType) type;
final DeclaredType declaredType = ununi( (DeclaredType) type );
final Element element = declaredType.asElement();
if ( element.getKind() == ElementKind.INTERFACE ) {
final Name name = ((TypeElement) element).getQualifiedName();

View File

@ -59,12 +59,14 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
comment( declaration );
modifiers( declaration );
preamble( declaration, returnType(), paramTypes );
chainSession( declaration );
nullChecks( paramTypes, declaration );
createCriteriaQuery( declaration );
where( declaration, paramTypes );
// orderBy( paramTypes, declaration );
executeQuery( declaration, paramTypes );
convertExceptions( declaration );
chainSessionEnd( false, declaration );
closingBrace( declaration );
return declaration.toString();
}
@ -102,7 +104,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
@Override
void createQuery(StringBuilder declaration) {
declaration
.append(sessionName)
.append(localSessionName())
.append(".createQuery(_query)\n");
}
@ -119,7 +121,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
private void createCriteriaQuery(StringBuilder declaration) {
declaration
.append("\tvar _builder = ")
.append(sessionName)
.append(localSessionName())
.append(isUsingEntityManager()
? ".getEntityManagerFactory()"
: ".getFactory()")

View File

@ -85,6 +85,7 @@ public class QueryMethod extends AbstractQueryMethod {
modifiers( paramTypes, declaration );
preamble( declaration, returnType, paramTypes );
collectOrdering( declaration, paramTypes );
chainSession( declaration );
tryReturn( declaration, paramTypes, containerType );
castResult( declaration, returnType );
createQuery( declaration );
@ -94,6 +95,7 @@ public class QueryMethod extends AbstractQueryMethod {
unwrapped = applyOrder( declaration, paramTypes, containerType, unwrapped );
execute( declaration, unwrapped );
convertExceptions( declaration );
chainSessionEnd( isUpdate, declaration );
closingBrace( declaration );
return declaration.toString();
}
@ -107,7 +109,7 @@ public class QueryMethod extends AbstractQueryMethod {
@Override
void createQuery(StringBuilder declaration) {
declaration
.append(sessionName)
.append(localSessionName())
.append(isNative ? ".createNativeQuery" : ".createQuery")
.append("(")
.append(getConstantName());

View File

@ -98,6 +98,7 @@ public final class Constants {
public static final String TUPLE = "jakarta.persistence.Tuple";
public static final String UNI = "io.smallrye.mutiny.Uni";
public static final String UNI_MUTINY_SESSION = UNI+"<org.hibernate.reactive.mutiny.Mutiny.Session>";
public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute";
public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";
@ -129,7 +130,8 @@ public final class Constants {
Constants.ENTITY_MANAGER,
Constants.HIB_SESSION,
Constants.HIB_STATELESS_SESSION,
Constants.MUTINY_SESSION
Constants.MUTINY_SESSION,
Constants.UNI_MUTINY_SESSION
);
//TODO: this is not even an exhaustive list of built-in basic types