fix and test for KeyedResultList with EntityManager

missing an unwrap()
also fix up some whitespace
This commit is contained in:
Gavin King 2024-03-20 20:26:08 +01:00 committed by Steve Ebersole
parent 527beb0bdb
commit 0d0f8754ca
4 changed files with 65 additions and 13 deletions

View File

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

View File

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

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
}
}

View File

@ -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<Book> booksFromDate(LocalDate minDate, KeyedPage<Book> page);
@Find
KeyedResultList<Book> booksWithTitleLike(@Pattern String title, KeyedPage<Book> page);
}