Skip to content

Commit 7126e07

Browse files
FroMagegavinking
authored andcommitted
[HHH-19586] Support Panache2
- Detect Panache2 in classpath - Detect Panache2 types for entities and repositories
1 parent aaeb65f commit 7126e07

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-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
@@ -104,6 +104,8 @@ public final class Context {
104104

105105
private boolean usesQuarkusOrm = false;
106106
private boolean usesQuarkusReactive = false;
107+
private boolean usesQuarkusPanache2 = false;
108+
private boolean usesQuarkusReactiveCommon = false;
107109

108110
private String[] includes = {"*"};
109111
private String[] excludes = {};
@@ -461,6 +463,22 @@ public boolean usesQuarkusReactive() {
461463
return usesQuarkusReactive;
462464
}
463465

466+
public void setUsesQuarkusPanache2(boolean b) {
467+
usesQuarkusPanache2 = b;
468+
}
469+
470+
public boolean usesQuarkusPanache2() {
471+
return usesQuarkusPanache2;
472+
}
473+
474+
public void setUsesQuarkusReactiveCommon(boolean b) {
475+
usesQuarkusReactiveCommon = b;
476+
}
477+
478+
public boolean usesQuarkusReactiveCommon() {
479+
return usesQuarkusReactiveCommon;
480+
}
481+
464482
public void setInclude(String include) {
465483
includes = include.split(",\\s*");
466484
}

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) ) {
@@ -284,6 +291,8 @@ && packagePresent(quarkusOrmPanachePackage) ) {
284291
context.setUsesQuarkusReactive( packagePresent(quarkusReactivePanachePackage) );
285292
context.setSpringInjection( packagePresent(springBeansPackage) );
286293
context.setAddComponentAnnotation( packagePresent(springStereotypePackage) );
294+
context.setUsesQuarkusPanache2( packagePresent(quarkusPanache2Package) );
295+
context.setUsesQuarkusReactiveCommon( packagePresent(quarkusReactivePanacheCommonPackage) );
287296

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

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

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -766,23 +766,28 @@ private void setupSession() {
766766
final ExecutableElement getter = findSessionGetter( element );
767767
if ( getter != null ) {
768768
// Never make a DAO for Panache subtypes
769-
if ( !isPanacheType( element ) ) {
769+
if ( !isPanacheType( element ) && !isPanache2Type( element ) ) {
770770
repository = true;
771771
sessionType = addDaoConstructor( getter );
772772
}
773-
else {
774-
// For Panache subtypes, we look at the session type, but no DAO,
773+
else if ( ! isPanache2Repository( element ) && !isPanache2Type( element ) ) {
774+
// For Panache 1 subtypes, we look at the session type, but no DAO,
775775
// we want static methods
776776
sessionType = fullReturnType(getter);
777777
}
778+
else {
779+
// For Panache 2 repositories we want a repository
780+
repository = true;
781+
sessionType = setupQuarkusDaoConstructor( getter, element );
782+
}
778783
}
779784
else if ( element.getKind() == ElementKind.INTERFACE
780785
&& !jakartaDataRepository
781-
&& ( context.usesQuarkusOrm() || context.usesQuarkusReactive() ) ) {
786+
&& ( context.usesQuarkusOrm() || context.usesQuarkusReactive() || context.usesQuarkusPanache2() ) ) {
782787
// if we don't have a getter, and not a JD repository, but we're in Quarkus,
783788
// we know how to find the default sessions
784789
repository = true;
785-
sessionType = setupQuarkusDaoConstructor();
790+
sessionType = setupQuarkusDaoConstructor( null, element );
786791
}
787792
if ( !repository && jakartaDataRepository ) {
788793
repository = true;
@@ -889,6 +894,19 @@ private boolean isReactivePanacheType(TypeElement type) {
889894
|| extendsClass( type, PANACHE_REACTIVE_ENTITY_BASE );
890895
}
891896

897+
private boolean isPanache2Type(TypeElement type) {
898+
return implementsInterface( type, PANACHE2_ENTITY_MARKER )
899+
|| isPanache2Repository( type );
900+
}
901+
902+
private boolean isPanache2Repository(TypeElement type) {
903+
return implementsInterface( type, PANACHE2_MANAGED_BLOCKING_REPOSITORY_BASE )
904+
|| implementsInterface( type, PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE )
905+
|| implementsInterface( type, PANACHE2_MANAGED_REACTIVE_REPOSITORY_BASE )
906+
|| implementsInterface( type, PANACHE2_STATELESS_REACTIVE_REPOSITORY_BASE )
907+
;
908+
}
909+
892910
/**
893911
* If there is a session getter method, we generate an instance
894912
* variable backing it, together with a constructor that initializes
@@ -930,10 +948,31 @@ private String addDaoConstructor(@Nullable ExecutableElement method) {
930948
/**
931949
* For Quarkus, we generate a constructor with injection for EntityManager in ORM,
932950
* and in HR, we define the static session getter.
951+
* For Panache 2, we can use the element to figure out what kind of session we want since this
952+
* is for repositories
933953
*/
934-
private String setupQuarkusDaoConstructor() {
935-
if ( context.usesQuarkusOrm() ) {
936-
String name = "getEntityManager";
954+
private String setupQuarkusDaoConstructor(@Nullable ExecutableElement getter, @Nullable TypeElement element) {
955+
if ( context.usesQuarkusOrm()
956+
|| (context.usesQuarkusPanache2()
957+
&& element != null
958+
&& (implementsInterface(element, PANACHE2_MANAGED_BLOCKING_REPOSITORY_BASE)
959+
|| implementsInterface(element, PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE)))
960+
) {
961+
String name;
962+
String sessionType;
963+
if ( getter != null ) {
964+
name = getter.getSimpleName().toString();
965+
sessionType = fullReturnType(getter);
966+
}
967+
else if(element != null
968+
&& implementsInterface(element, PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE)) {
969+
name = "getStatelessSession";
970+
sessionType = HIB_STATELESS_SESSION;
971+
}
972+
else { // good default
973+
name = "getSession";
974+
sessionType = HIB_SESSION;
975+
}
937976
putMember( name,
938977
new RepositoryConstructor(
939978
this,
@@ -949,13 +988,20 @@ private String setupQuarkusDaoConstructor() {
949988
true
950989
)
951990
);
952-
return ENTITY_MANAGER;
991+
return sessionType;
953992
}
954993
else {
955994
importType( Constants.QUARKUS_SESSION_OPERATIONS );
956995
// use this getter to get the method, do not generate an injection point for its type
957-
sessionGetter = "SessionOperations.getSession()";
958-
return UNI_MUTINY_SESSION;
996+
if(element != null
997+
&& implementsInterface(element, PANACHE2_STATELESS_REACTIVE_REPOSITORY_BASE)) {
998+
sessionGetter = "SessionOperations.getStatelessSession()";
999+
return UNI_MUTINY_STATELESS_SESSION;
1000+
}
1001+
else {
1002+
sessionGetter = "SessionOperations.getSession()";
1003+
return UNI_MUTINY_SESSION;
1004+
}
9591005
}
9601006
}
9611007

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
@@ -162,6 +162,12 @@ public final class Constants {
162162
public static final String SPRING_STATELESS_SESSION_PROVIDER = SPRING_OBJECT_PROVIDER + "<" + HIB_STATELESS_SESSION + ">";
163163
public static final String SPRING_COMPONENT = "org.springframework.stereotype.Component";
164164

165+
public static final String PANACHE2_ENTITY_MARKER = "io.quarkus.hibernate.panache.PanacheEntityMarker";
166+
public static final String PANACHE2_MANAGED_BLOCKING_REPOSITORY_BASE = "io.quarkus.hibernate.panache.managed.blocking.PanacheManagedBlockingRepositoryBase";
167+
public static final String PANACHE2_STATELESS_BLOCKING_REPOSITORY_BASE = "io.quarkus.hibernate.panache.stateless.blocking.PanacheStatelessBlockingRepositoryBase";
168+
public static final String PANACHE2_MANAGED_REACTIVE_REPOSITORY_BASE = "io.quarkus.hibernate.panache.managed.reactive.PanacheManagedReactiveRepositoryBase";
169+
public static final String PANACHE2_STATELESS_REACTIVE_REPOSITORY_BASE = "io.quarkus.hibernate.panache.stateless.reactive.PanacheStatelessReactiveRepositoryBase";
170+
165171
public static final Map<String, String> COLLECTIONS = Map.of(
166172
COLLECTION, COLLECTION_ATTRIBUTE,
167173
SET, 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;
@@ -100,7 +101,7 @@ void testQuarkusRepositoryMetamodel() throws Exception {
100101
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
101102

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

0 commit comments

Comments
 (0)