英文:
SQLGrammarException: could not execute statement while trying to delete from db
问题
我正在尝试解决一个关于删除语句查询的问题。现在的实现看起来是这样的。
@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);
现在我遇到了这个错误:
could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
我尝试了添加 @Modifying
注解,然后又尝试移除它,尝试了各种 @Transactional
和 @Modifying
的组合,仍然遇到了与这个删除操作有关的各种错误,比如:
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
或者 Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
现在我真的不确定问题出在哪里。
英文:
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.
答案1
得分: 1
用以下代码替换你的删除查询:
@Modifying
@Query(value = "DELETE FROM generated_statements WHERE created_date < :expiryDate", nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
或者直接将其转换为JPQL/HQL查询,不需要使用nativeQuery。示例:
@Modifying
@Query(value = "DELETE FROM GeneratedStatement g WHERE g.createDate < :expiryDate")
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
通过指定int
返回值,你将自动获得已删除行数的数量。
英文:
Replace your delete query with this
@Modifying
@Query(value = "DELETE FROM generated_statements WHERE created_date < :expiryDate", nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
or turn it directly into a JPQL/HQL query. No need for a nativeQuery. Example:
@Modifying
@Query(value = "DELETE FROM GeneratedStatement g WHERE g.createDate < :expiryDate")
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
By specifying an int
return value, you will automatically get the number of deleted rows.
答案2
得分: 0
考虑到所有这些复杂的逻辑只是为了获取已删除行的数量,您可以尝试以下方式:
<!-- language: lang-java -->
@Modifying
@Query("DELETE FROM generated_statements WHERE created_date < :expiryDate")
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
或者只需使用关键词(如果您的实体具有createdDate属性)
int deleteByCreatedDateBefore(LocalDate expiryDate);
英文:
Considering all this complex logic is to get number of deleted rows you can try just:
<!-- language: lang-java -->
@Modifying
@Query("DELETE FROM generated_statements WHERE created_date < :expiryDate")
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
or just using keywords(if your entity has createdDate attribute)
int deleteByCreatedDateBefore(LocalDate expiryDate);
答案3
得分: 0
@Modifying
@Query(value = "DELETE FROM generated_statements WHERE created_date < ?1", nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
英文:
@Modifying
@Query(value = "DELETE FROM generated_statements WHERE created_date < ?1", nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论