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" ); 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]; final NativeSQLQueryReturn nativeSQLQueryReturn = queryReturns[0];
if ( nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn ) { if ( nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn ) {

View File

@ -19,6 +19,7 @@ import javax.persistence.Query;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.NativeQuery; import org.hibernate.query.NativeQuery;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -26,6 +27,7 @@ import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
/** /**
* @author Andrea Boriero * @author Andrea Boriero
@ -33,11 +35,11 @@ import static org.junit.Assert.assertEquals;
@TestForIssue(jiraKey = "HHH-11092") @TestForIssue(jiraKey = "HHH-11092")
public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase { public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase {
private static final String[] GAME_TITLES = {"Halo", "Grand Theft Auto", "NetHack"}; private static final String[] GAME_TITLES = { "Halo", "Grand Theft Auto", "NetHack" };
@Override @Override
public Class[] getAnnotatedClasses() { public Class[] getAnnotatedClasses() {
return new Class[] {Game.class}; return new Class[] { Game.class };
} }
@Before @Before
@ -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") @Entity(name = "Game")
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1")) @NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1"))
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?")) @NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))