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? "); .append(".requestTotal()\n\t\t\t\t\t\t? ");
createQuery( declaration ); createQuery( declaration );
setParameters( declaration, paramTypes, "\t\t\t\t\t"); setParameters( declaration, paramTypes, "\t\t\t\t\t");
if ( isUsingEntityManager() ) {
declaration
.append("\t\t\t\t\t");
}
unwrapQuery( declaration, !isUsingEntityManager() ); unwrapQuery( declaration, !isUsingEntityManager() );
declaration declaration
.append("\t\t\t\t\t\t\t\t.getResultCount()\n\t\t\t\t\t\t: -1;\n"); .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, @Nullable String containerType,
boolean unwrapped, boolean unwrapped,
boolean mustUnwrap) { boolean mustUnwrap) {
declaration
.append("\t\t\t");
if ( containerType == null ) { if ( containerType == null ) {
declaration declaration
.append(".getSingleResult();"); .append("\t\t\t.getSingleResult();");
} }
else { else {
switch (containerType) { switch (containerType) {
@ -618,32 +620,33 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
} }
else { else {
declaration 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(annotationMetaEntity.importType(returnTypeName))
.append("[0]);"); .append("[0]);");
} }
break; break;
case OPTIONAL: case OPTIONAL:
declaration declaration
.append(".uniqueResultOptional();"); .append("\t\t\t.uniqueResultOptional();");
break; break;
case STREAM: case STREAM:
declaration declaration
.append(".getResultStream();"); .append("\t\t\t.getResultStream();");
break; break;
case LIST: case LIST:
declaration declaration
.append(".getResultList();"); .append("\t\t\t.getResultList();");
break; break;
case HIB_KEYED_RESULT_LIST: case HIB_KEYED_RESULT_LIST:
unwrapQuery(declaration, unwrapped);
declaration declaration
.append(".getKeyedResultList(") .append("\t\t\t.getKeyedResultList(")
.append(parameterName(HIB_KEYED_PAGE, paramTypes, paramNames)) .append(parameterName(HIB_KEYED_PAGE, paramTypes, paramNames))
.append(");"); .append(");");
break; break;
case JD_PAGE: case JD_PAGE:
declaration declaration
.append(".getResultList();\n") .append("\t\t\t.getResultList();\n")
.append("\t\treturn new ") .append("\t\treturn new ")
.append(annotationMetaEntity.importType("jakarta.data.page.impl.PageRecord")) .append(annotationMetaEntity.importType("jakarta.data.page.impl.PageRecord"))
.append('(') .append('(')
@ -655,8 +658,9 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
throw new AssertionFailure("entity class cannot be null"); throw new AssertionFailure("entity class cannot be null");
} }
else { else {
unwrapQuery(declaration, unwrapped);
declaration declaration
.append(".getKeyedResultList(_keyedPage);\n"); .append("\t\t\t.getKeyedResultList(_keyedPage);\n");
annotationMetaEntity.importType("jakarta.data.page.PageRequest"); annotationMetaEntity.importType("jakarta.data.page.PageRequest");
annotationMetaEntity.importType("jakarta.data.page.PageRequest.Cursor"); annotationMetaEntity.importType("jakarta.data.page.PageRequest.Cursor");
annotationMetaEntity.importType("jakarta.data.page.impl.CursoredPageRecord"); annotationMetaEntity.importType("jakarta.data.page.impl.CursoredPageRecord");
@ -672,13 +676,16 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
default: default:
if ( isUsingEntityManager() && !unwrapped && mustUnwrap ) { if ( isUsingEntityManager() && !unwrapped && mustUnwrap ) {
declaration declaration
.append("\t\t\t") .append("\t\t\t.unwrap(")
.append(".unwrap(")
.append(annotationMetaEntity.importType(containerType)) .append(annotationMetaEntity.importType(containerType))
.append(".class);"); .append(".class);");
} }
else { else {
final int lastIndex = declaration.length() - 1;
if ( declaration.charAt(lastIndex) == '\n' ) {
declaration.setLength(lastIndex);
}
declaration.append(';'); declaration.append(';');
} }
} }

View File

@ -3,7 +3,6 @@ package org.hibernate.processor.test.keypage;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import org.hibernate.annotations.processing.Find; import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL; import org.hibernate.annotations.processing.HQL;
import org.hibernate.processor.test.hqlsql.Book;
import org.hibernate.query.KeyedPage; import org.hibernate.query.KeyedPage;
import org.hibernate.query.KeyedResultList; 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);
}