HHH-17960: Fix proper default session type for JD repositories even in Quarkus

And tests
This commit is contained in:
Stéphane Épardaud 2024-04-18 15:43:27 +02:00 committed by Gavin King
parent 7c8690592e
commit 1948c8f2bd
12 changed files with 185 additions and 12 deletions

View File

@ -72,6 +72,7 @@ dependencies {
quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2" quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2"
jakartaDataImplementation "jakarta.data:jakarta.data-api:1.0.0-SNAPSHOT" jakartaDataImplementation "jakarta.data:jakarta.data-api:1.0.0-SNAPSHOT"
jakartaDataImplementation "org.hibernate.reactive:hibernate-reactive-core:2.2.2.Final" jakartaDataImplementation "org.hibernate.reactive:hibernate-reactive-core:2.2.2.Final"
jakartaDataImplementation "io.quarkus:quarkus-hibernate-orm-panache:3.6.2"
} }
// The source set gets a custom configuration which extends the normal test implementation config // The source set gets a custom configuration which extends the normal test implementation config

View File

@ -0,0 +1,6 @@
package io.quarkus.hibernate.orm.panache;
// workaround until Quarkus is published with this class
public interface _PanacheEntity {
}

View File

@ -0,0 +1,18 @@
package org.hibernate.processor.test.data.quarkus;
import java.util.List;
import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.Find;
import jakarta.data.repository.Query;
import jakarta.data.repository.Repository;
@Repository
public interface JakartaDataBookRepository extends CrudRepository<PanacheBook, Long> {
@Find
public List<PanacheBook> findBook(String isbn);
@Query("WHERE isbn = :isbn")
public List<PanacheBook> hqlBook(String isbn);
}

View File

@ -0,0 +1,25 @@
package org.hibernate.processor.test.data.quarkus;
import java.util.List;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;
@Entity
public class PanacheBook extends PanacheEntity {
public @NaturalId String isbn;
public @NaturalId String title;
public @NaturalId String author;
public String text;
public int pages;
@Find
public static native List<PanacheBook> findBook(String isbn);
@HQL("WHERE isbn = :isbn")
public static native List<PanacheBook> hqlBook(String isbn);
}

View File

@ -0,0 +1,62 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.processor.test.data.quarkus;
import org.hibernate.StatelessSession;
import org.hibernate.processor.test.util.CompilationTest;
import org.hibernate.processor.test.util.TestUtil;
import org.hibernate.processor.test.util.WithClasses;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import static org.hibernate.processor.test.util.TestUtil.getMetamodelClassFor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* @author Gavin King
*/
public class QuarkusOrmPanacheTest extends CompilationTest {
@Test
@WithClasses({ PanacheBook.class, JakartaDataBookRepository.class })
public void testJakartaDataRepositoryMetamodel() throws Exception {
// JD repository
System.out.println( TestUtil.getMetaModelSourceAsString( JakartaDataBookRepository.class ) );
Class<?> repositoryClass = getMetamodelClassFor( JakartaDataBookRepository.class );
Assertions.assertNotNull( repositoryClass );
// Make sure it has the proper supertype
Class<?> superclass = repositoryClass.getSuperclass();
if ( superclass != null ) {
Assertions.assertEquals( "java.lang.Object", superclass.getName() );
}
Class<?>[] interfaces = repositoryClass.getInterfaces();
Assertions.assertEquals( 1, interfaces.length );
Assertions.assertEquals( JakartaDataBookRepository.class.getName(), interfaces[0].getName() );
// Annotated method generates an instance method
Method method = repositoryClass.getDeclaredMethod( "hqlBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Annotated method generates an instance method
method = repositoryClass.getDeclaredMethod( "findBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Make sure we have the proper constructor
Constructor<?> constructor = repositoryClass.getDeclaredConstructor( StatelessSession.class );
Assertions.assertNotNull( constructor );
Assertions.assertTrue( constructor.isAnnotationPresent( Inject.class ) );
}
}

View File

@ -597,8 +597,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
else if ( element.getKind() == ElementKind.INTERFACE else if ( element.getKind() == ElementKind.INTERFACE
&& !jakartaDataRepository
&& ( context.usesQuarkusOrm() || context.usesQuarkusReactive() ) ) { && ( context.usesQuarkusOrm() || context.usesQuarkusReactive() ) ) {
// if we don't have a getter, but we're in Quarkus, we know how to find the default sessions // 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
repository = true; repository = true;
sessionType = setupQuarkusDaoConstructor(); sessionType = setupQuarkusDaoConstructor();
} }

View File

@ -0,0 +1,6 @@
package io.quarkus.hibernate.reactive.panache;
// workaround until Quarkus is published with this class
public interface _PanacheEntity {
}

View File

@ -0,0 +1,24 @@
// Not supported yet: https://hibernate.atlassian.net/browse/HHH-17960
//package org.hibernate.processor.test.hrPanache;
//
//import java.util.List;
//
//import org.hibernate.reactive.mutiny.Mutiny;
//
//import io.smallrye.mutiny.Uni;
//import jakarta.data.repository.CrudRepository;
//import jakarta.data.repository.Find;
//import jakarta.data.repository.Query;
//import jakarta.data.repository.Repository;
//
//@Repository
//public interface JakartaDataBookRepository extends CrudRepository<PanacheBook, Long> {
//
// public Uni<Mutiny.StatelessSession> session();
//
// @Find
// public Uni<List<PanacheBook>> findBook(String isbn);
//
// @Query("WHERE isbn = :isbn")
// public Uni<List<PanacheBook>> hqlBook(String isbn);
//}

View File

@ -1,8 +1,5 @@
package org.hibernate.processor.test.hrPanache; package org.hibernate.processor.test.hrPanache;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.util.List; import java.util.List;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
@ -11,10 +8,11 @@ import org.hibernate.annotations.processing.HQL;
import io.quarkus.hibernate.reactive.panache.PanacheEntity; import io.quarkus.hibernate.reactive.panache.PanacheEntity;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import jakarta.persistence.Entity;
@Entity @Entity
public class PanacheBook extends PanacheEntity { public class PanacheBook extends PanacheEntity {
public @Id String isbn; public @NaturalId String isbn;
public @NaturalId String title; public @NaturalId String title;
public @NaturalId String author; public @NaturalId String author;
public String text; public String text;

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.processor.test.hrPanache; package org.hibernate.processor.test.hrPanache;
import org.hibernate.StatelessSession;
import org.hibernate.processor.test.util.CompilationTest; import org.hibernate.processor.test.util.CompilationTest;
import org.hibernate.processor.test.util.TestUtil; import org.hibernate.processor.test.util.TestUtil;
import org.hibernate.processor.test.util.WithClasses; import org.hibernate.processor.test.util.WithClasses;
@ -13,6 +14,7 @@ import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import static org.hibernate.processor.test.util.TestUtil.getMetamodelClassFor; import static org.hibernate.processor.test.util.TestUtil.getMetamodelClassFor;
@ -145,5 +147,39 @@ public class QuarkusHrPanacheTest extends CompilationTest {
// Make sure we do not override the default session method // Make sure we do not override the default session method
Assertions.assertThrows( NoSuchMethodException.class, () -> repositoryClass.getDeclaredMethod( "mySession" ) ); Assertions.assertThrows( NoSuchMethodException.class, () -> repositoryClass.getDeclaredMethod( "mySession" ) );
} }
// Not supported yet: https://hibernate.atlassian.net/browse/HHH-17960
// @Test
// @WithClasses({ PanacheBook.class, JakartaDataBookRepository.class })
// public void testJakartaDataRepositoryMetamodel() throws Exception {
// // JD repository
// System.out.println( TestUtil.getMetaModelSourceAsString( JakartaDataBookRepository.class ) );
// Class<?> repositoryClass = getMetamodelClassFor( JakartaDataBookRepository.class );
// Assertions.assertNotNull( repositoryClass );
//
// // Make sure it has the proper supertype
// Class<?> superclass = repositoryClass.getSuperclass();
// if ( superclass != null ) {
// Assertions.assertEquals( "java.lang.Object", superclass.getName() );
// }
// Class<?>[] interfaces = repositoryClass.getInterfaces();
// Assertions.assertEquals( 1, interfaces.length );
// Assertions.assertEquals( JakartaDataBookRepository.class.getName(), interfaces[0].getName() );
//
// // Annotated method generates an instance method
// Method method = repositoryClass.getDeclaredMethod( "hqlBook", String.class );
// Assertions.assertNotNull( method );
// Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
//
// // Annotated method generates an instance method
// method = repositoryClass.getDeclaredMethod( "findBook", String.class );
// Assertions.assertNotNull( method );
// Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
//
// // Make sure we have the proper constructor
// Constructor<?> constructor = repositoryClass.getDeclaredConstructor( StatelessSession.class );
// Assertions.assertNotNull( constructor );
// Assertions.assertTrue( constructor.isAnnotationPresent( Inject.class ) );
// }
} }

View File

@ -1,8 +1,5 @@
package org.hibernate.processor.test.ormPanache; package org.hibernate.processor.test.ormPanache;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.util.List; import java.util.List;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
@ -10,10 +7,11 @@ import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL; import org.hibernate.annotations.processing.HQL;
import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;
@Entity @Entity
public class PanacheBook extends PanacheEntity { public class PanacheBook extends PanacheEntity {
public @Id String isbn; public @NaturalId String isbn;
public @NaturalId String title; public @NaturalId String title;
public @NaturalId String author; public @NaturalId String author;
public String text; public String text;

View File

@ -21,9 +21,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
/** // Note: JD test is in jakartaData tests, due to requiring Java 17
* @author Gavin King
*/
public class QuarkusOrmPanacheTest extends CompilationTest { public class QuarkusOrmPanacheTest extends CompilationTest {
@Test @Test
@WithClasses({ PanacheBook.class }) @WithClasses({ PanacheBook.class })