使用多个where子句删除spring数据jpa

时间:2018-01-30 11:37:41

标签: mysql hibernate spring-boot spring-data-jpa jpql

我想从MySQL表中删除记录。

我在扩展JpaRepositoy的界面中编写的查询是。

@Modifying
@Transactional
@Query(value="DELETE FROM tablename WHERE end_date>=?1 and username=?2 and start_date <=?3)
void deleteByStart_dateAndUsernameAndEnd_date(Date start_date,String username,Date end_date);

我想传递start_date,它小于或等于end_date,end_date大于或等于start_date

我得到以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [delete from com.poc.pa.model.tablename where end_date>=?1 and username=?2 and start_date <=?3]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [delete from com.poc.pa.model.tablename where end_date>=?1 and username=?2 and start_date <=?3]

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我假设delete语句是SQL查询。因此,您必须在Query注释中声明这一点。只需添加 nativeQuery = true

即可
@Transactional    
@Modifying
@Query(value="DELETE FROM tablename WHERE end_date>=?1 and username=?2 and start_date <=?3, 
    nativeQuery = true)
void deleteByStart_dateAndUsernameAndEnd_date(Date start_date,String username,Date end_date);