JpaRepository Not supported for DML operations [delete query]
Try this:
public interface LimitRepository extends JpaRepository<CLimit, Long> { @Transactional @Modifying @Query("delete from CLimit l where l.trader.id =:#{#trader.id}") void deleteLimitsByTrader(@Param("trader") CTrader trader);}
Whenever you are trying to modify a record in db, you have to mark it @Transactional
as well as @Modifying
, which instruct Spring that it can modify existing records.
The modifying queries can only use void or int/Integer as return type, otherwise it will throw an exception.
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@Transactionalpublic 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<CLimit, Long> { @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.
You forgot to add two annotations
above of method .
@Transactional@Modifying