Skip to content

Commit 787f0a4

Browse files
Dariush MoshiriSanne
authored andcommitted
HHH-11413: Native named query creation fails unintuitively when no resultClass is specified
1 parent 0325cd6 commit 787f0a4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ else if ( namedQueryDefinition.getResultSetRef() != null ) {
966966
throw new IllegalArgumentException( "Cannot create TypedQuery for query with more than one return" );
967967
}
968968

969+
if ( queryReturns.length == 0 ) {
970+
throw new IllegalArgumentException("Named query exists but its result type is not compatible");
971+
}
972+
969973
final NativeSQLQueryReturn nativeSQLQueryReturn = queryReturns[0];
970974

971975
if ( nativeSQLQueryReturn instanceof NativeSQLQueryRootReturn ) {

hibernate-core/src/test/java/org/hibernate/jpa/test/query/NamedQueryTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,27 @@
1919
import org.hibernate.Session;
2020
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
2121
import org.hibernate.query.NativeQuery;
22+
2223
import org.hibernate.testing.TestForIssue;
2324
import org.junit.After;
2425
import org.junit.Before;
2526
import org.junit.Test;
2627

2728
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
2829
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertThrows;
2931

3032
/**
3133
* @author Andrea Boriero
3234
*/
3335
@TestForIssue(jiraKey = "HHH-11092")
3436
public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase {
3537

36-
private static final String[] GAME_TITLES = {"Halo", "Grand Theft Auto", "NetHack"};
38+
private static final String[] GAME_TITLES = { "Halo", "Grand Theft Auto", "NetHack" };
3739

3840
@Override
3941
public Class[] getAnnotatedClasses() {
40-
return new Class[] {Game.class};
42+
return new Class[] { Game.class };
4143
}
4244

4345
@Before
@@ -178,6 +180,18 @@ public void testNativeQueriesFromNamedQueriesDoNotShareQuerySpaces() {
178180
} );
179181
}
180182

183+
@Test
184+
@TestForIssue(jiraKey = "HHH-11413")
185+
public void testNamedNativeQueryExceptionNoRedultDefined() {
186+
doInJPA( this::entityManagerFactory, entityManager -> {
187+
assertThrows(
188+
"Named query exists but its result type is not compatible",
189+
IllegalArgumentException.class,
190+
() -> entityManager.createNamedQuery( "NamedNativeQuery", Game.class )
191+
);
192+
} );
193+
}
194+
181195
@Entity(name = "Game")
182196
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1"))
183197
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))

0 commit comments

Comments
 (0)