diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/AuxiliaryTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/AuxiliaryTest.java index ea91569d15..ebabeac0f0 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/AuxiliaryTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/AuxiliaryTest.java @@ -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]); } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/Book.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/Book.java index bc6ff7c8b4..7a70af676b 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/Book.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/Book.java @@ -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", diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/TitleAndIsbn.java b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/TitleAndIsbn.java new file mode 100644 index 0000000000..1dfe6bb362 --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/namedquery/TitleAndIsbn.java @@ -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; + } +}