Skip to content

[3.0] Syntax error when using setMaxResults() on native queries with SQL Server / PostgreSQL #2316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2025
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 @@ -11,7 +11,6 @@
import java.util.Set;
import java.util.concurrent.CompletionStage;

import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.results.ResultSetMapping;
import org.hibernate.query.spi.DomainQueryExecutionContext;
Expand All @@ -23,7 +22,6 @@
import org.hibernate.query.sql.spi.ParameterOccurrence;
import org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter;
import org.hibernate.reactive.engine.spi.ReactiveSharedSessionContractImplementor;
import org.hibernate.reactive.pool.impl.Parameters;
import org.hibernate.reactive.query.internal.ReactiveResultSetMappingProcessor;
import org.hibernate.reactive.query.spi.ReactiveNativeSelectQueryPlan;
import org.hibernate.reactive.sql.exec.internal.StandardReactiveSelectExecutor;
Expand Down Expand Up @@ -61,8 +59,7 @@ public ReactiveNativeSelectQueryPlanImpl(
resultSetMapping.addAffectedTableNames( affectedTableNames, sessionFactory );
}
this.affectedTableNames = affectedTableNames;
Dialect dialect = sessionFactory.getJdbcServices().getDialect();
this.sql = Parameters.instance( dialect ).process( parser.process() );
this.sql = parser.process();
this.parameterList = parameterList;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.hibernate.HibernateException;
import org.hibernate.LockOptions;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand All @@ -22,6 +23,7 @@
import org.hibernate.reactive.logging.impl.Log;
import org.hibernate.reactive.logging.impl.LoggerFactory;
import org.hibernate.reactive.pool.ReactiveConnection;
import org.hibernate.reactive.pool.impl.Parameters;
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
import org.hibernate.reactive.session.ReactiveSession;
import org.hibernate.reactive.util.impl.CompletionStages;
Expand Down Expand Up @@ -172,6 +174,11 @@ private CompletionStage<ResultSet> executeQuery() {
return completedFuture( logicalConnection )
.thenCompose( lg -> {
LOG.tracef( "Executing query to retrieve ResultSet : %s", getFinalSql() );

Dialect dialect = executionContext.getSession().getJdbcServices().getDialect();
// This must happen at the very last minute in order to process parameters
// added in org.hibernate.dialect.pagination.OffsetFetchLimitHandler.processSql
final String sql = Parameters.instance( dialect ).process( getFinalSql() );
Object[] parameters = PreparedStatementAdaptor.bind( super::bindParameters );

final SessionEventListenerManager eventListenerManager = executionContext
Expand All @@ -181,7 +188,7 @@ private CompletionStage<ResultSet> executeQuery() {

eventListenerManager.jdbcExecuteStatementStart();
return connection()
.selectJdbc( getFinalSql(), parameters )
.selectJdbc( sql, parameters )
.thenCompose( this::validateResultSet )
.whenComplete( (resultSet, throwable) -> {
// FIXME: I don't know if this event makes sense for Vert.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static jakarta.persistence.FetchType.LAZY;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.hibernate.reactive.QueryTest.Author.AUTHOR_TABLE;
import static org.hibernate.reactive.QueryTest.Author.HQL_NAMED_QUERY;
import static org.hibernate.reactive.QueryTest.Author.SQL_NAMED_QUERY;
Expand Down Expand Up @@ -395,6 +396,42 @@ public void testNativeEntityQueryWithParam(VertxTestContext context) {
);
}

// https://github.com/hibernate/hibernate-reactive/issues/2314
@Test
public void testNativeEntityQueryWithLimit(VertxTestContext context) {
Author author1 = new Author( "Iain M. Banks" );
Author author2 = new Author( "Neal Stephenson" );
Book book1 = new Book( "1-85723-235-6", "Feersum Endjinn", author1 );
Book book2 = new Book( "0-380-97346-4", "Cryptonomicon", author2 );
Book book3 = new Book( "0-553-08853-X", "Snow Crash", author2 );
author1.books.add( book1 );
author2.books.add( book2 );
author2.books.add( book3 );

test(
context,
openSession()
.thenCompose( session -> session.persist( author1, author2 )
.thenCompose( v -> session.flush() )
)
.thenCompose( v -> openSession() )
.thenCompose( session -> session.createNativeQuery(
"select * from " + BOOK_TABLE + " order by isbn",
Book.class
)
.setMaxResults( 2 )
.getResultList() )
.thenAccept( books -> {
assertThat( books )
.extracting( b -> b.id, b -> b.title, b -> b.isbn )
.containsExactly(
tuple( book2.id, book2.title, book2.isbn ),
tuple( book3.id, book3.title, book3.isbn )
);
} )
);
}

@Test
public void testNativeEntityQueryWithNamedParam(VertxTestContext context) {
Author author1 = new Author( "Iain M. Banks" );
Expand Down
Loading