HHH-15263 - @NamedQuery is not supported with UPDATE statement

This commit is contained in:
Nathan Xu 2022-05-10 22:57:51 -04:00 committed by Christian Beikov
parent 9d35e3dd18
commit 3630fbad9b
2 changed files with 22 additions and 3 deletions

View File

@ -817,14 +817,14 @@ public class QuerySqmImpl<R>
@Override
public SqmQueryImplementor<R> setLockOptions(LockOptions lockOptions) {
verifySelect();
// No verifySelect call, because in Hibernate we support locking in subqueries
getQueryOptions().getLockOptions().overlay( lockOptions );
return this;
}
@Override
public SqmQueryImplementor<R> setLockMode(String alias, LockMode lockMode) {
verifySelect();
// No verifySelect call, because in Hibernate we support locking in subqueries
getQueryOptions().getLockOptions().setAliasSpecificLockMode( alias, lockMode );
return this;
}
@ -869,6 +869,7 @@ public class QuerySqmImpl<R>
@Override
public SqmQueryImplementor<R> setLockMode(LockModeType lockMode) {
if ( lockMode != LockModeType.NONE ) {
// JPA requires an exception to be thrown when this is not a select statement
verifySelect();
}
getSession().checkOpen( false );

View File

@ -8,6 +8,7 @@ package org.hibernate.orm.test.annotations.query;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -24,6 +25,7 @@ import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Query;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -71,6 +73,19 @@ public class NamedQueryTest {
);
}
@Test
@TestForIssue( jiraKey = "HHH-15263" )
public void testNoExceptionThrownForNamedUpdate(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Query query = session.getNamedQuery( "NamedUpdate" );
query.setParameter( 1, GAME_TITLES[0] + " 2" );
query.setParameter( 2, GAME_TITLES[0] );
assertDoesNotThrow( () -> query.executeUpdate(), "without fixing, 'java.lang.IllegalStateException: Expecting a SELECT query' exception would be thrown" );
}
);
}
@Test
public void testNativeNamedQueriesOrdinalParametersAreOneBased(SessionFactoryScope scope) {
scope.inTransaction(
@ -84,7 +99,10 @@ public class NamedQueryTest {
}
@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"),
@NamedQuery(name = "NamedUpdate", query = "update Game set title = ?1 where title = ?2")
})
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))
public static class Game {
private Long id;