JpaRepository Not supported for DML operations [delete query]

前端 未结 4 409
南笙
南笙 2020-12-13 05:39

I have written a query to delete some objects in my interface extending JPaRepository, but when I execute the query it throws an exception! Can anyone explain

4条回答
  •  攒了一身酷
    2020-12-13 05:55

    I had the same issue and I tried @afridi's answer which is working fine but bad practice, as far as I understand. you should not use @Transactional annotation in the repository class but service(and implementation) classes. please find the below answer.

    LimitServiceImpl.java

    import org.springframework.transaction.annotation.Transactional;
    ...
    @Override
    @Transactional
    public void deleteLimitsByTrader(CTrader trader) {
    // here im calling the LimitRepository interface. 
     getEntityRepository().deleteLimitsByTrader(trader);
    }
    

    LimitRepository.java

    import org.springframework.data.jpa.repository.Modifying;
    import org.springframework.data.jpa.repository.Query;
    ...
    public interface LimitRepository extends JpaRepository {
    
      @Modifying
      @Query("delete from CLimit l where l.trader.id =:#{#trader.id}")
      void deleteLimitsByTrader(@Param("trader") CTrader trader);
    
    }
    

    make sure to use the correct imports.

提交回复
热议问题