Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tooling/metamodel-generator/hibernate-jpamodelgen.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2"
jakartaDataImplementation "jakarta.data:jakarta.data-api:1.0.0-SNAPSHOT"
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
Expand Down
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.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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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 ) );
}
}
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
@@ -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 @@ -21,9 +21,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
* @author Gavin King
*/
// Note: JD test is in jakartaData tests, due to requiring Java 17
public class QuarkusOrmPanacheTest extends CompilationTest {
@Test
@WithClasses({ PanacheBook.class })
Expand Down