diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java index 6ab15a568a..8bbac93640 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java @@ -471,6 +471,10 @@ public abstract class AbstractQueryMethod implements MetaAttribute { .append(".requestTotal()\n\t\t\t\t\t\t? "); createQuery( declaration ); setParameters( declaration, paramTypes, "\t\t\t\t\t"); + if ( isUsingEntityManager() ) { + declaration + .append("\t\t\t\t\t"); + } unwrapQuery( declaration, !isUsingEntityManager() ); declaration .append("\t\t\t\t\t\t\t\t.getResultCount()\n\t\t\t\t\t\t: -1;\n"); @@ -604,11 +608,9 @@ public abstract class AbstractQueryMethod implements MetaAttribute { @Nullable String containerType, boolean unwrapped, boolean mustUnwrap) { - declaration - .append("\t\t\t"); if ( containerType == null ) { declaration - .append(".getSingleResult();"); + .append("\t\t\t.getSingleResult();"); } else { switch (containerType) { @@ -618,32 +620,33 @@ public abstract class AbstractQueryMethod implements MetaAttribute { } else { declaration - .append(".getResultList()\n\t\t\t.toArray(new ") + .append("\t\t\t.getResultList()\n\t\t\t.toArray(new ") .append(annotationMetaEntity.importType(returnTypeName)) .append("[0]);"); } break; case OPTIONAL: declaration - .append(".uniqueResultOptional();"); + .append("\t\t\t.uniqueResultOptional();"); break; case STREAM: declaration - .append(".getResultStream();"); + .append("\t\t\t.getResultStream();"); break; case LIST: declaration - .append(".getResultList();"); + .append("\t\t\t.getResultList();"); break; case HIB_KEYED_RESULT_LIST: + unwrapQuery(declaration, unwrapped); declaration - .append(".getKeyedResultList(") + .append("\t\t\t.getKeyedResultList(") .append(parameterName(HIB_KEYED_PAGE, paramTypes, paramNames)) .append(");"); break; case JD_PAGE: declaration - .append(".getResultList();\n") + .append("\t\t\t.getResultList();\n") .append("\t\treturn new ") .append(annotationMetaEntity.importType("jakarta.data.page.impl.PageRecord")) .append('(') @@ -655,8 +658,9 @@ public abstract class AbstractQueryMethod implements MetaAttribute { throw new AssertionFailure("entity class cannot be null"); } else { + unwrapQuery(declaration, unwrapped); declaration - .append(".getKeyedResultList(_keyedPage);\n"); + .append("\t\t\t.getKeyedResultList(_keyedPage);\n"); annotationMetaEntity.importType("jakarta.data.page.PageRequest"); annotationMetaEntity.importType("jakarta.data.page.PageRequest.Cursor"); annotationMetaEntity.importType("jakarta.data.page.impl.CursoredPageRecord"); @@ -672,13 +676,16 @@ public abstract class AbstractQueryMethod implements MetaAttribute { default: if ( isUsingEntityManager() && !unwrapped && mustUnwrap ) { declaration - .append("\t\t\t") - .append(".unwrap(") + .append("\t\t\t.unwrap(") .append(annotationMetaEntity.importType(containerType)) .append(".class);"); } else { + final int lastIndex = declaration.length() - 1; + if ( declaration.charAt(lastIndex) == '\n' ) { + declaration.setLength(lastIndex); + } declaration.append(';'); } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Dao.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Dao.java index f67fef4a7a..33edf71106 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Dao.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Dao.java @@ -3,7 +3,6 @@ package org.hibernate.processor.test.keypage; import jakarta.persistence.EntityManager; import org.hibernate.annotations.processing.Find; import org.hibernate.annotations.processing.HQL; -import org.hibernate.processor.test.hqlsql.Book; import org.hibernate.query.KeyedPage; import org.hibernate.query.KeyedResultList; diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/KeyBasedTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/KeyBasedTest.java new file mode 100644 index 0000000000..c8ac2114d1 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/KeyBasedTest.java @@ -0,0 +1,29 @@ +/* + * 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 . + */ +package org.hibernate.processor.test.keypage; + +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 static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor; + +/** + * @author Gavin King + */ +public class KeyBasedTest extends CompilationTest { + @Test + @WithClasses({ Book.class, Dao.class, Queries.class }) + public void testQueryMethod() { + System.out.println( TestUtil.getMetaModelSourceAsString( Dao.class ) ); + System.out.println( TestUtil.getMetaModelSourceAsString( Queries.class ) ); + assertMetamodelClassGeneratedFor( Book.class ); + assertMetamodelClassGeneratedFor( Dao.class ); + assertMetamodelClassGeneratedFor( Queries.class ); + } +} diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Queries.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Queries.java new file mode 100644 index 0000000000..cc976551a1 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/keypage/Queries.java @@ -0,0 +1,17 @@ +package org.hibernate.processor.test.keypage; + +import org.hibernate.annotations.processing.Find; +import org.hibernate.annotations.processing.HQL; +import org.hibernate.annotations.processing.Pattern; +import org.hibernate.query.KeyedPage; +import org.hibernate.query.KeyedResultList; + +import java.time.LocalDate; + +public interface Queries { + @HQL("where publicationDate > :minDate") + KeyedResultList booksFromDate(LocalDate minDate, KeyedPage page); + + @Find + KeyedResultList booksWithTitleLike(@Pattern String title, KeyedPage page); +}