HHH-11413: Native named query creation fails unintuitively when no resultClass is specified

This commit is contained in:
Dariush Moshiri 2021-07-24 17:01:29 +02:00 committed by Sanne Grinovero
parent 0325cd632a
commit 787f0a44ea
2 changed files with 20 additions and 2 deletions

View File

@ -966,6 +966,10 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
throw new IllegalArgumentException( "Cannot create TypedQuery for query with more than one return" );
}
if ( queryReturns.length == 0 ) {
throw new IllegalArgumentException("Named query exists but its result type is not compatible");
}
final NativeSQLQueryReturn nativeSQLQueryReturn = queryReturns[0];
if ( nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn ) {

View File

@ -19,6 +19,7 @@ import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.NativeQuery;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
@ -26,6 +27,7 @@ import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
/**
* @author Andrea Boriero
@ -178,6 +180,18 @@ public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase {
} );
}
@Test
@TestForIssue(jiraKey = "HHH-11413")
public void testNamedNativeQueryExceptionNoRedultDefined() {
doInJPA( this::entityManagerFactory, entityManager -> {
assertThrows(
"Named query exists but its result type is not compatible",
IllegalArgumentException.class,
() -> entityManager.createNamedQuery( "NamedNativeQuery", Game.class )
);
} );
}
@Entity(name = "Game")
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1"))
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))