Spring Data Jpa出现Not supported for DML operations

Problem description: When using the annotation form of Spring Data Jpa to configure and delete SQL statements, the following exception occurred:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations

The annotation form of Spring Data Jpa to configure delete sql statement is as follows:

​@Query("delete from User where id=:idStr")
void delete(@Param("idStr") String idStr);

Solution:

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

Add @Modifying annotation to the above sql configuration

@Modifying​
@Query("delete from User where id=:idStr")
void delete(@Param("idStr") String idStr);

The @Modifying  annotation often appears in JPA to inform jpa that this is an update or delete operation. This annotation must be added during the update or delete operation, otherwise an exception will be thrown

Guess you like

Origin blog.csdn.net/y_bccl27/article/details/109637825