add better test for TypedQueryReference in metamodel

This commit is contained in:
Steve Ebersole 2024-11-14 08:31:46 -06:00 committed by Gavin King
parent ef3250cb28
commit 0b856f531d
3 changed files with 57 additions and 0 deletions

View File

@ -5,14 +5,20 @@
package org.hibernate.processor.test.namedquery;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQueryReference;
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 java.lang.reflect.ParameterizedType;
import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor;
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfMethodInMetamodelFor;
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfNameFieldInMetamodelFor;
import static org.hibernate.processor.test.util.TestUtil.getFieldFromMetamodelFor;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Gavin King
@ -129,5 +135,31 @@ public class AuxiliaryTest extends CompilationTest {
Object.class,
Object.class
);
checkTypedQueryReference( "QUERY_TITLES_WITH_ISBNS", "_titlesWithIsbns_", Object[].class );
checkTypedQueryReference( "QUERY_TITLES_AND_ISBNS_AS_RECORD", "_titlesAndIsbnsAsRecord_", TitleAndIsbn.class );
checkTypedQueryReference( "QUERY_TYPE_OF_BOOK", "__typeOfBook_", Type.class );
checkTypedQueryReference( "QUERY_GET_TITLES", "__getTitles_", String.class );
checkTypedQueryReference( "QUERY_GET_UPPER_LOWER_TITLES", "__getUpperLowerTitles_", Object[].class );
checkTypedQueryReference( "QUERY_BOOKS_BY_TITLE", "_booksByTitle_", Book.class );
checkTypedQueryReference( "QUERY_BOOKS_BY_TITLE_VERBOSE", "_booksByTitleVerbose_", Book.class );
}
private static void checkTypedQueryReference(String NAME, String name, Class<?> type) {
assertPresenceOfNameFieldInMetamodelFor(
Book.class,
NAME,
"Missing named query attribute."
);
assertPresenceOfFieldInMetamodelFor(
Book.class,
name,
"Missing typed query reference."
);
ParameterizedType fieldType = (ParameterizedType)
getFieldFromMetamodelFor( Book.class, name )
.getGenericType();
assertEquals(TypedQueryReference.class, fieldType.getRawType());
assertEquals( type, fieldType.getActualTypeArguments()[0]);
}
}

View File

@ -23,6 +23,8 @@ import jakarta.persistence.NamedQuery;
query = "select book from Book book where book.title like :titlePattern and book.type = :type")
@NamedQuery(name = "#getTitles",
query = "select title from Book")
@NamedQuery(name = "titlesAndIsbnsAsRecord",
query = "select new org.hibernate.processor.test.namedquery.TitleAndIsbn(title,isbn) from Book")
@NamedQuery(name = "#getUpperLowerTitles",
query = "select upper(title), lower(title), length(title) from Book")
@NamedQuery(name = "#typeOfBook",

View File

@ -0,0 +1,23 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.namedquery;
public final class TitleAndIsbn {
private final String title;
private final String isbn;
public TitleAndIsbn(String title, String isbn) {
this.title = title;
this.isbn = isbn;
}
public String title() {
return title;
}
public String isbn() {
return isbn;
}
}