Skip to content

Native Query with clause and delete broken in spring-data-jpa 3.4.x #3817

Closed as not planned
@markxj

Description

@markxj

In springboot 3.3.9 (using spring-data-jpa 3.3.9 and hibernate 6.5.3), a with clause supports delete operations - e.g.

@Transactional
@Modifying
@Query(nativeQuery = true,
    value = """
    with to_delete as (select id from person where birthday < :birthday)
            delete from person where id in (select id from to_delete)
    """)
int deleteByBirthday(@Param("birthday") Date birthday);

In springboot 3.4.3 (using spring-data-jpa 3.4.3 and hibernate 6.6.8), this same query generates the following error

Syntax error in SQL statement "with to_delete as (select id from person where birthday < ?)\000a [*]delete from person where id in (select id from to_delete)\000a"; expected ",, (, SELECT, TABLE, VALUES"; SQL statement:
with to_delete as (select id from person where birthday < ?)
delete from person where id in (select id from to_delete)

The attached sample project shows the issue - just change the springboot version in the pom

In my real world scenario I use this form of query in a loop where I also add an order by and limit clause so I do not overload my DB trying to delete 1000s of rows in a single transaction.

The springboot team also overrode my sample to use Hibernate 6.5.3, and it still failed, so the error appears to be in spring-data-jpa - ref spring-projects/spring-boot#44744

with-clause.zip

Metadata

Metadata

Assignees

Labels

for: external-projectFor an external project and not something we can fix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions