I'm trying to solve a problem with statement deletion query. Now the implementation looks like this.
@Transactional
public void deleteStatements(LocalDate expiryDate) {
int deletedStatements = statementRepository.deleteByIdCreatedDateBefore(expiryDate);
logger.info("Deleted {} statements.", deletedStatements);
}
@Query(value = "WITH deleted AS (DELETE FROM generated_statements WHERE created_date < :expiryDate RETURNING id) " +
"SELECT count(*) FROM deleted;", nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
Now I'm getting this error:
could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
I tried adding @Modifying annotation, then I tried removing it, tried with various combinations of @Transactional and @Modifying, still got various errors regarding this deletion like:
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet or Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
Now I'm really not sure what is the problem here.
CTEat all? JDBC will automatically return the number of deleted rows and I would expect that an obfuscation layer like JPA can actually return that information.