Skip to content

Native query Pageable query in correctly created #3634

@mveelaid

Description

@mveelaid

Native Query Pagination Incorrectly Modifies String Literals

Issue Description

When using a native query with pagination in Spring Data JPA, the pagination clause fetch first ? rows only is incorrectly inserted into string literals within the query, breaking the SQL syntax.

Reproducible Example

@Repository
public interface TestRepository extends PagingAndSortingRepository<Test, String> {
    @Query(value = """
            SELECT ';'
            FROM test
            """,
            countQuery = """
            SELECT count(*)
            FROM test
            """,
            nativeQuery = true)
    Page<QTestSearchRow> searchTest(Pageable pageable);
}

Expected Query

SELECT ';'
FROM test
fetch first ? rows only;

Actual Query

SELECT 'fetch first ? rows only;'
FROM test;

Impact

This issue affects any native query that contains string literals with semicolons, particularly impacting queries that use:

  • String concatenation
  • String aggregation (STRING_AGG in PostgreSQL)
  • Complex string manipulations

Environment

  • Spring Boot version: 3.3.4
  • Spring Data JPA version: 3.3.4
  • Database: PostgreSQL 16.4

Additional Notes

This is particularly problematic when using functions like STRING_AGG where semicolons might be used as delimiters. For example:

STRING_AGG(column_name, ';')

would incorrectly become:

STRING_AGG(column_name, 'fetch first ? rows only;')

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions