Skip to content

Commit

Permalink
Auto-detect quarkus and default to dao with the proper session type
Browse files Browse the repository at this point in the history
Unless there's a different session getter defined
  • Loading branch information
FroMage authored and gavinking committed Mar 1, 2024
1 parent 5b184ca commit b32296f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public final class Context {
// keep track of which named queries have been checked
private final Set<String> checkedNamedQueries = new HashSet<>();

private boolean usesQuarkusOrm = false;
private boolean usesQuarkusReactive = false;

public Context(ProcessingEnvironment processingEnvironment) {
this.processingEnvironment = processingEnvironment;

Expand Down Expand Up @@ -390,4 +393,20 @@ public String toString() {
public boolean checkNamedQuery(String name) {
return checkedNamedQueries.add(name);
}

public void setUsesQuarkusOrm(boolean b) {
usesQuarkusOrm = b;
}

public boolean usesQuarkusOrm() {
return usesQuarkusOrm;
}

public void setUsesQuarkusReactive(boolean b) {
usesQuarkusReactive = b;
}

public boolean usesQuarkusReactive() {
return usesQuarkusReactive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,29 @@ private boolean handleSettings(ProcessingEnvironment environment) {
context.getProcessingEnvironment().getElementUtils()
.getPackageElement( "io.quarkus.hibernate.orm" );

PackageElement quarkusOrmPanachePackage =
context.getProcessingEnvironment().getElementUtils()
.getPackageElement( "io.quarkus.hibernate.orm.panache" );
PackageElement quarkusReactivePanachePackage =
context.getProcessingEnvironment().getElementUtils()
.getPackageElement( "io.quarkus.hibernate.reactive.panache" );
if ( quarkusReactivePanachePackage != null
&& quarkusOrmPanachePackage != null ) {
context.logMessage(
Diagnostic.Kind.WARNING,
"Both Quarkus Hibernate ORM and Hibernate Reactive with Panache detected: this is not supported, so will proceed as if none were there"
);
quarkusOrmPanachePackage = quarkusReactivePanachePackage = null;
}

context.setAddInjectAnnotation( jakartaInjectPackage != null );
context.setAddNonnullAnnotation( jakartaAnnotationPackage != null );
context.setAddGeneratedAnnotation( jakartaAnnotationPackage != null );
context.setAddDependentAnnotation( jakartaContextPackage != null );
context.setAddTransactionScopedAnnotation( jakartaTransactionsPackage != null );
context.setQuarkusInjection( quarkusOrmPackage != null );
context.setUsesQuarkusOrm( quarkusOrmPanachePackage != null );
context.setUsesQuarkusReactive( quarkusReactivePanachePackage != null );

final Map<String, String> options = environment.getOptions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import jakarta.persistence.EntityManager;

import static java.beans.Introspector.decapitalize;
import static java.lang.Boolean.FALSE;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -407,6 +409,12 @@ private void setupSession() {
sessionType = getter.getReturnType().toString();
}
}
else if ( element.getKind() == ElementKind.INTERFACE
&& ( context.usesQuarkusOrm() || context.usesQuarkusReactive() ) ) {
// if we don't have a getter, but we're in Quarkus, we know how to find the default sessions
repository = true;
sessionType = setupQuarkusDaoConstructor();
}
if ( !repository && jakartaDataRepository ) {
repository = true;
sessionType = HIB_STATELESS_SESSION;
Expand Down Expand Up @@ -514,6 +522,41 @@ private String addDaoConstructor(@Nullable ExecutableElement method) {
return sessionType;
}

/**
* For Quarkus, we generate a constructor with injection for EntityManager in ORM,
* and in HR, we define the static session getter.
*/
private String setupQuarkusDaoConstructor() {
final String typeName = element.getSimpleName().toString() + '_';
final String sessionVariableName = getSessionVariableName( sessionType );

if ( context.usesQuarkusOrm() ) {
String name = "getEntityManager";
putMember( name,
new RepositoryConstructor(
this,
typeName,
name,
sessionType,
sessionVariableName,
dataStore(),
context.addInjectAnnotation(),
context.addNonnullAnnotation(),
false,
false,
true
)
);
return Constants.ENTITY_MANAGER;
}
else {
importType( Constants.QUARKUS_SESSION_OPERATIONS );
// use this getter to get the method, do not generate an injection point for its type
sessionGetter = "SessionOperations.getSession()";
return Constants.UNI_MUTINY_SESSION;
}
}

/**
* The session getter method doesn't have to be a JavaBeans-style
* getter. It can be any method with no parameters and one of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public final class Constants {
public static final String HIB_SESSION_FACTORY = "org.hibernate.SessionFactory";
public static final String HIB_STATELESS_SESSION = "org.hibernate.StatelessSession";
public static final String MUTINY_SESSION = "org.hibernate.reactive.mutiny.Mutiny.Session";
public static final String QUARKUS_SESSION_OPERATIONS = "io.quarkus.hibernate.reactive.panache.common.runtime.SessionOperations";

public static final String TUPLE = "jakarta.persistence.Tuple";

Expand Down

0 comments on commit b32296f

Please sign in to comment.