Skip to content

A native query with paging and semicolon is generated incorrectly #3785

@AlexanderShchelkunov

Description

@AlexanderShchelkunov

Description

Hello!

When I have a native query with paging and semicolon, Spring Data generates incorrect query (it inserts 'fetch first ? rows only' before the semicolon):

SELECT *
FROM users
WHERE login = ' fetch first ? rows only;' OR login IS NULL

Example of the query:

  @Query(
      value = """
          SELECT *
          FROM users
          WHERE login = ';' OR login IS NULL
          """,
      nativeQuery = true
  )
  Page<UserEntity> findUsers(Pageable pageable);

In real application I have regexp_split_to_array function with semicolon (PostgreSQL).
It worked fine in Spring ~2.6.3.

Environment

Spring Boot: 3.3.9
Java: JDK 17

Steps to Reproduce

To reproduce the defect run the app and the error will occur during @PostConstruct execution in UserService

Error Message

	at com.example.demo.DemoApplication.main(DemoApplication.java:10) ~[main/:na]
Caused by: org.springframework.dao.DataIntegrityViolationException: JDBC exception executing SQL [SELECT *
FROM users
WHERE login = ' fetch first ? rows only;' OR login IS NULL

Minimal sample application

See the attachment:
demo-native-query.zip

Workaround

Pass ';' to the query as a parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions