Skip to content

Commit

Permalink
HHH-17960: Fix proper default session type for JD repositories even i…
Browse files Browse the repository at this point in the history
…n Quarkus

And tests
  • Loading branch information
FroMage committed Apr 18, 2024
1 parent aae9b1e commit 4bd2ea8
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 9 deletions.
2 changes: 2 additions & 0 deletions tooling/metamodel-generator/hibernate-jpamodelgen.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ dependencies {
xjc rootProject.fileTree(dir: 'patched-libs/jaxb2-basics', include: '*.jar')

quarkusOrmPanacheImplementation "io.quarkus:quarkus-hibernate-orm-panache:3.6.2"
quarkusOrmPanacheImplementation "jakarta.data:jakarta.data-api:1.0.0-SNAPSHOT"
quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2"
quarkusHrPanacheImplementation "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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ private void setupSession() {
}
}
else if ( element.getKind() == ElementKind.INTERFACE
&& !jakartaDataRepository
&& ( 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;
sessionType = setupQuarkusDaoConstructor();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.hibernate.reactive.panache;

// workaround until Quarkus is published with this class
public interface _PanacheEntity {

}
Original file line number Diff line number Diff line change
@@ -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);
//}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.hibernate.processor.test.hrPanache;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import java.util.List;

import org.hibernate.annotations.NaturalId;
Expand All @@ -11,10 +8,11 @@

import io.quarkus.hibernate.reactive.panache.PanacheEntity;
import io.smallrye.mutiny.Uni;
import jakarta.persistence.Entity;

@Entity
public class PanacheBook extends PanacheEntity {
public @Id String isbn;
public @NaturalId String isbn;
public @NaturalId String title;
public @NaturalId String author;
public String text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
*/
package org.hibernate.processor.test.hrPanache;

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 io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;

import static org.hibernate.processor.test.util.TestUtil.getMetamodelClassFor;

Expand Down Expand Up @@ -145,5 +147,39 @@ public void testBookRepositoryWithSessionMetamodel() throws Exception {
// Make sure we do not override the default session method
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 ) );
// }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.hibernate.orm.panache;

// workaround until Quarkus is published with this class
public interface _PanacheEntity {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.hibernate.processor.test.ormPanache;

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);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.hibernate.processor.test.ormPanache;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

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 @Id String isbn;
public @NaturalId String isbn;
public @NaturalId String title;
public @NaturalId String author;
public String text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.processor.test.ormPanache;

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;
Expand Down Expand Up @@ -107,4 +108,37 @@ public void testQuarkusRepositoryMetamodel() throws Exception {
Assertions.assertNotNull( constructor );
Assertions.assertTrue( constructor.isAnnotationPresent( Inject.class ) );
}

@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 ) );
}
}

0 comments on commit 4bd2ea8

Please sign in to comment.