Skip to content

Commit 7205879

Browse files
committed
Support Panache2
- Detect Panache2 in classpath - Detect Panache2 types for entities and repositories
1 parent f07a6f4 commit 7205879

File tree

5 files changed

+89
-12
lines changed

5 files changed

+89
-12
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public final class Context {
102102

103103
private boolean usesQuarkusOrm = false;
104104
private boolean usesQuarkusReactive = false;
105+
private boolean usesQuarkusPanache2 = false;
106+
private boolean usesQuarkusReactiveCommon = false;
105107

106108
private String[] includes = {"*"};
107109
private String[] excludes = {};
@@ -443,6 +445,22 @@ public boolean usesQuarkusReactive() {
443445
return usesQuarkusReactive;
444446
}
445447

448+
public void setUsesQuarkusPanache2(boolean b) {
449+
usesQuarkusPanache2 = b;
450+
}
451+
452+
public boolean usesQuarkusPanache2() {
453+
return usesQuarkusPanache2;
454+
}
455+
456+
public void setUsesQuarkusReactiveCommon(boolean b) {
457+
usesQuarkusReactiveCommon = b;
458+
}
459+
460+
public boolean usesQuarkusReactiveCommon() {
461+
return usesQuarkusReactiveCommon;
462+
}
463+
446464
public void setInclude(String include) {
447465
includes = include.split(",\\s*");
448466
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,16 @@ private boolean handleSettings(ProcessingEnvironment environment) {
253253
PackageElement quarkusOrmPanachePackage =
254254
context.getProcessingEnvironment().getElementUtils()
255255
.getPackageElement( "io.quarkus.hibernate.orm.panache" );
256+
PackageElement quarkusPanache2Package =
257+
context.getProcessingEnvironment().getElementUtils()
258+
.getPackageElement( "io.quarkus.hibernate.panache" );
256259
PackageElement quarkusReactivePanachePackage =
257260
context.getProcessingEnvironment().getElementUtils()
258261
.getPackageElement( "io.quarkus.hibernate.reactive.panache" );
262+
// This is imported automatically by Quarkus extensions when HR is also imported
263+
PackageElement quarkusReactivePanacheCommonPackage =
264+
context.getProcessingEnvironment().getElementUtils()
265+
.getPackageElement( "io.quarkus.hibernate.reactive.panache.common" );
259266

260267
if ( packagePresent(quarkusReactivePanachePackage)
261268
&& packagePresent(quarkusOrmPanachePackage) ) {
@@ -275,6 +282,8 @@ && packagePresent(quarkusOrmPanachePackage) ) {
275282
context.setQuarkusInjection( packagePresent(quarkusOrmPackage) || packagePresent(quarkusReactivePackage) );
276283
context.setUsesQuarkusOrm( packagePresent(quarkusOrmPanachePackage) );
277284
context.setUsesQuarkusReactive( packagePresent(quarkusReactivePanachePackage) );
285+
context.setUsesQuarkusPanache2( packagePresent(quarkusPanache2Package) );
286+
context.setUsesQuarkusReactiveCommon( packagePresent(quarkusReactivePanacheCommonPackage) );
278287

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

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -764,21 +764,26 @@ private void setupSession() {
764764
final ExecutableElement getter = findSessionGetter( element );
765765
if ( getter != null ) {
766766
// Never make a DAO for Panache subtypes
767-
if ( !isPanacheType( element ) ) {
767+
if ( !isPanacheType( element ) && !isPanache2Type( element ) ) {
768768
repository = true;
769769
sessionType = addDaoConstructor( getter );
770770
}
771-
else {
772-
// For Panache subtypes, we look at the session type, but no DAO, we want static methods
771+
else if ( ! isPanache2Repository( element ) && !isPanache2Type( element ) ) {
772+
// For Panache 1 subtypes, we look at the session type, but no DAO, we want static methods
773773
sessionType = fullReturnType(getter);
774774
}
775+
else {
776+
// For Panache 2 repositories we want a repository
777+
repository = true;
778+
sessionType = setupQuarkusDaoConstructor( getter, element );
779+
}
775780
}
776781
else if ( element.getKind() == ElementKind.INTERFACE
777782
&& !jakartaDataRepository
778-
&& ( context.usesQuarkusOrm() || context.usesQuarkusReactive() ) ) {
783+
&& ( context.usesQuarkusOrm() || context.usesQuarkusReactive() || context.usesQuarkusPanache2() ) ) {
779784
// if we don't have a getter, and not a JD repository, but we're in Quarkus, we know how to find the default sessions
780785
repository = true;
781-
sessionType = setupQuarkusDaoConstructor();
786+
sessionType = setupQuarkusDaoConstructor( null, element );
782787
}
783788
if ( !repository && jakartaDataRepository ) {
784789
repository = true;
@@ -878,6 +883,19 @@ private boolean isReactivePanacheType(TypeElement type) {
878883
|| extendsClass( type, PANACHE_REACTIVE_ENTITY_BASE );
879884
}
880885

886+
private boolean isPanache2Type(TypeElement type) {
887+
return implementsInterface( type, PANACHE2_ENTITY_MARKER )
888+
|| isPanache2Repository( type );
889+
}
890+
891+
private boolean isPanache2Repository(TypeElement type) {
892+
return implementsInterface( type, PANACHE2_MANAGED_BLOCKING_REPOSITORY_BASE )
893+
|| implementsInterface( type, PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE )
894+
|| implementsInterface( type, PANACHE2_MANAGED_REACTIVE_REPOSITORY_BASE )
895+
|| implementsInterface( type, PANACHE2_STATELESS_REACTIVE_REPOSITORY_BASE )
896+
;
897+
}
898+
881899
/**
882900
* If there is a session getter method, we generate an instance
883901
* variable backing it, together with a constructor that initializes
@@ -915,10 +933,29 @@ private String addDaoConstructor(@Nullable ExecutableElement method) {
915933
/**
916934
* For Quarkus, we generate a constructor with injection for EntityManager in ORM,
917935
* and in HR, we define the static session getter.
936+
* For Panache 2, we can use the element to figure out what kind of session we want since this
937+
* is for repositories
918938
*/
919-
private String setupQuarkusDaoConstructor() {
920-
if ( context.usesQuarkusOrm() ) {
921-
String name = "getEntityManager";
939+
private String setupQuarkusDaoConstructor(@Nullable ExecutableElement getter, @Nullable TypeElement element) {
940+
if ( context.usesQuarkusOrm()
941+
|| (context.usesQuarkusPanache2()
942+
&& element != null
943+
&& (implementsInterface(element, PANACHE2_MANAGED_BLOCKING_REPOSITORY_BASE)
944+
|| implementsInterface(element, PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE)))
945+
) {
946+
String name;
947+
String sessionType;
948+
if ( getter != null ) {
949+
name = getter.getSimpleName().toString();
950+
sessionType = fullReturnType(getter);
951+
} else if(element != null
952+
&& implementsInterface(element, PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE)) {
953+
name = "getStatelessSession";
954+
sessionType = HIB_STATELESS_SESSION;
955+
} else { // good default
956+
name = "getSession";
957+
sessionType = HIB_SESSION;
958+
}
922959
putMember( name,
923960
new RepositoryConstructor(
924961
this,
@@ -934,13 +971,19 @@ private String setupQuarkusDaoConstructor() {
934971
true
935972
)
936973
);
937-
return ENTITY_MANAGER;
974+
return sessionType;
938975
}
939976
else {
940977
importType( Constants.QUARKUS_SESSION_OPERATIONS );
941978
// use this getter to get the method, do not generate an injection point for its type
942-
sessionGetter = "SessionOperations.getSession()";
943-
return Constants.UNI_MUTINY_SESSION;
979+
if(element != null
980+
&& implementsInterface(element, PANACHE2_STATELESS_REACTIVE_REPOSITORY_BASE)) {
981+
sessionGetter = "SessionOperations.getStatelessSession()";
982+
return UNI_MUTINY_STATELESS_SESSION;
983+
} else {
984+
sessionGetter = "SessionOperations.getSession()";
985+
return Constants.UNI_MUTINY_SESSION;
986+
}
944987
}
945988
}
946989

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ public final class Constants {
158158
public static final String PANACHE_REACTIVE_REPOSITORY_BASE = "io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase";
159159
public static final String PANACHE_REACTIVE_ENTITY_BASE = "io.quarkus.hibernate.reactive.panache.PanacheEntityBase";
160160

161+
public static final String PANACHE2_ENTITY_MARKER = "io.quarkus.hibernate.panache.PanacheEntityMarker";
162+
public static final String PANACHE2_MANAGED_BLOCKING_REPOSITORY_BASE = "io.quarkus.hibernate.panache.managed.blocking.PanacheManagedBlockingRepositoryBase";
163+
public static final String PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE = "io.quarkus.hibernate.panache.stateless.blocking.PanacheStatelessBlockingRepositoryBase";
164+
public static final String PANACHE2_MANAGED_REACTIVE_REPOSITORY_BASE = "io.quarkus.hibernate.panache.managed.reactive.PanacheManagedReactiveRepositoryBase";
165+
public static final String PANACHE2_STATELESS_REACTIVE_REPOSITORY_BASE = "io.quarkus.hibernate.panache.stateless.reactive.PanacheStatelessReactiveRepositoryBase";
166+
161167
public static final Map<String, String> COLLECTIONS = Map.of(
162168
COLLECTION, Constants.COLLECTION_ATTRIBUTE,
163169
SET, Constants.SET_ATTRIBUTE,

tooling/metamodel-generator/src/quarkusOrmPanache/java/org/hibernate/processor/test/ormPanache/QuarkusOrmPanacheTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.processor.test.ormPanache;
66

7+
import org.hibernate.Session;
78
import org.hibernate.processor.test.util.CompilationTest;
89
import org.hibernate.processor.test.util.TestUtil;
910
import org.hibernate.processor.test.util.WithClasses;
@@ -99,7 +100,7 @@ public void testQuarkusRepositoryMetamodel() throws Exception {
99100
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
100101

101102
// Make sure we have the proper constructor
102-
Constructor<?> constructor = repositoryClass.getDeclaredConstructor( EntityManager.class );
103+
Constructor<?> constructor = repositoryClass.getDeclaredConstructor( Session.class );
103104
Assertions.assertNotNull( constructor );
104105
Assertions.assertTrue( constructor.isAnnotationPresent( Inject.class ) );
105106
}

0 commit comments

Comments
 (0)