SQLGrammarException:尝试从数据库中删除时无法执行语句

huangapple go评论95阅读模式
英文:

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(&quot;Deleted {} statements.&quot;, deletedStatements);
}


@Query(value = &quot;WITH deleted AS (DELETE FROM generated_statements WHERE created_date &lt; :expiryDate RETURNING id) &quot; +
        &quot;SELECT count(*) FROM deleted;&quot;, 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 = &quot;DELETE FROM generated_statements WHERE created_date &lt; :expiryDate&quot;, nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);

or turn it directly into a JPQL/HQL query. No need for a nativeQuery. Example:

@Modifying
@Query(value = &quot;DELETE FROM GeneratedStatement g WHERE g.createDate &lt; :expiryDate&quot;)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);

By specifying an int return value, you will automatically get the number of deleted rows.

答案2

得分: 0

考虑到所有这些复杂的逻辑只是为了获取已删除行的数量,您可以尝试以下方式:

&lt;!-- language: lang-java --&gt;

    @Modifying
    @Query(&quot;DELETE FROM generated_statements WHERE created_date &lt; :expiryDate&quot;)
    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(&quot;DELETE FROM generated_statements WHERE created_date &lt; :expiryDate&quot;)
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 = &quot;DELETE FROM generated_statements WHERE created_date &lt; ?1&quot;, nativeQuery = true)
int deleteByIdCreatedDateBefore(LocalDate expiryDate);

huangapple
  • 本文由 发表于 2020年4月6日 17:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定