我想从此方法中删除本机查询。我该怎么做?
I want to remove native query from this method. How do I do it?
@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "DELETE FROM transaction WHERE transaction_cre_sys_date <= (now() - interval 6 month)",nativeQuery = true)
void deleteSixMonthOldTransactions();
这是我的交易实体。我想删除超过 6 个月的记录
public class Transaction {
private Integer id;
private String transactionSource;
private String transactionId;
private LocalDateTime transactionCreSysDate;
}
您的评论似乎表明您想使用派生查询创建与本机查询相同的效果。
这是不可能的。
如果从字面上看你只是不想使用 SQL,这意味着使用 JPQL。
问题是 JPQL 处理日期的功能非常有限,而且似乎不包括从时间戳中添加或减去月份的方法。
所以你可以做的是使用 Spring 表达式语言来传递日期的截断。
这里是 canonical blog post how to use SpEL in Spring Data Queries.
这里有一些关于如何在 SpEL 中操作日期的讨论。
How do I do date manipulation in SpEL?
但真正的答案是:只保留 SQL.
@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "DELETE FROM transaction WHERE transaction_cre_sys_date <= (now() - interval 6 month)",nativeQuery = true)
void deleteSixMonthOldTransactions();
这是我的交易实体。我想删除超过 6 个月的记录
public class Transaction {
private Integer id;
private String transactionSource;
private String transactionId;
private LocalDateTime transactionCreSysDate;
}
您的评论似乎表明您想使用派生查询创建与本机查询相同的效果。
这是不可能的。
如果从字面上看你只是不想使用 SQL,这意味着使用 JPQL。
问题是 JPQL 处理日期的功能非常有限,而且似乎不包括从时间戳中添加或减去月份的方法。
所以你可以做的是使用 Spring 表达式语言来传递日期的截断。
这里是 canonical blog post how to use SpEL in Spring Data Queries.
这里有一些关于如何在 SpEL 中操作日期的讨论。 How do I do date manipulation in SpEL?
但真正的答案是:只保留 SQL.