Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,20 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
if ( !isJakartaDataStyle()
&& statement instanceof SqmSelectStatement<?> selectStatement ) {
if ( isQueryMethodName( name ) ) {
final AnnotationValue annotationValue = getAnnotationValue( mirror, "resultClass" );
final String resultType = annotationValue != null
? annotationValue.getValue().toString()
: resultType( selectStatement, context );
putMember( name,
// TODO: respect @NamedQuery(resultClass)
new NamedQueryMethod(
this,
selectStatement,
name.substring(1),
isRepository(),
getSessionType(),
getSessionVariableName(),
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
resultType
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.TreeSet;

import static org.hibernate.processor.util.StringUtil.nameToFieldName;
import static org.hibernate.processor.util.SqmTypeUtils.resultType;

/**
* @author Gavin King
Expand All @@ -28,6 +27,7 @@ class NamedQueryMethod implements MetaAttribute {
private final boolean reactive;
private final String sessionVariableName;
private final boolean addNonnullAnnotation;
private final String resultClass;

public NamedQueryMethod(
AnnotationMeta annotationMeta,
Expand All @@ -36,14 +36,16 @@ public NamedQueryMethod(
boolean belongsToRepository,
@Nullable String sessionType,
String sessionVariableName,
boolean addNonnullAnnotation) {
boolean addNonnullAnnotation,
String resultClass) {
this.annotationMeta = annotationMeta;
this.select = select;
this.name = name;
this.belongsToRepository = belongsToRepository;
this.reactive = Constants.MUTINY_SESSION.equals(sessionType);
this.sessionVariableName = sessionVariableName;
this.addNonnullAnnotation = addNonnullAnnotation;
this.resultClass = resultClass;
}

@Override
Expand Down Expand Up @@ -72,7 +74,7 @@ public String getAttributeDeclarationString() {
.append(".createNamedQuery(")
.append(fieldName())
.append(", ")
.append( annotationMeta.importType( resultType( select, annotationMeta.getContext() ) ) )
.append( annotationMeta.importType( resultClass ) )
.append( ".class)");
for ( SqmParameter<?> param : sortedParameters ) {
declaration
Expand Down Expand Up @@ -123,7 +125,7 @@ private void returnType(StringBuilder declaration) {
declaration
.append(annotationMeta.importType(Constants.LIST))
.append('<')
.append( annotationMeta.importType( resultType( select, annotationMeta.getContext() ) ) )
.append( annotationMeta.importType( resultClass ) )
.append("> ")
.append(name);
if ( reactive ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.resultclass;

public record NameValue(String name, Integer value) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.resultclass;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;

@Entity
@NamedQuery(name = "#getNameValue", query = "select p.name, p.value from Post p", resultClass = NameValue.class)
public class Post {
@Id
Integer id;

String name;

Integer value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.processor.test.resultclass;

import jakarta.persistence.EntityManager;
import org.assertj.core.api.Assertions;
import org.hibernate.processor.test.util.CompilationTest;
import org.hibernate.processor.test.util.WithClasses;
import org.junit.Test;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import static org.hibernate.processor.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.processor.test.util.TestUtil.assertPresenceOfMethodInMetamodelFor;
import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString;
import static org.hibernate.processor.test.util.TestUtil.getMethodFromMetamodelFor;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ResultClassTest extends CompilationTest {
@Test
@WithClasses({Post.class, NameValue.class})
public void test() {
System.out.println( getMetaModelSourceAsString( Post.class ) );
assertMetamodelClassGeneratedFor( Post.class );

assertPresenceOfMethodInMetamodelFor( Post.class, "getNameValue", EntityManager.class );
final Method method = getMethodFromMetamodelFor( Post.class, "getNameValue", EntityManager.class );
if ( method.getGenericReturnType() instanceof ParameterizedType parameterized ) {
assertEquals( List.class, parameterized.getRawType() );
assertEquals( NameValue.class, parameterized.getActualTypeArguments()[0] );
}
else {
Assertions.fail();
}
}
}
Loading