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
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.