我在 JPA 中使用自定义查询,它不允许我使用 interval
关键字。如果我在查询中不使用 - interval '7 days'
,它会给出正确的输出。
异常说:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: interval near line 1, column 214
@Query("select d from DomainName d , DomainNameReminder dr, Reseller r"+
" where d.reminder.id = dr.id "+
" and dr.secondNotification=current_date - interval '7 days' "+
" and r.checkParty=true "+
" and r.id = d.invoicingParty.id ")
public List<Object> findDomainsBySecondNotificationDate();
这个查询基本上带来了今天前 7 天有第二次通知日期的所有记录。
我的接口(interface)声明为
public interface DomainNameRepository extends JpaRepository<DomainName, Long>,
QueryDslPredicateExecutor<DomainName> {
我的查询在 pgadmin postgresql 客户端
中给出了正确的输出,我很惊讶为什么我不能在这里使用关键字。
最佳答案
这解决了我的问题。
我使用了 nativeQuery=true
并使用了我在 postgresql 中执行的查询。也想与他人分享。
@Query( nativeQuery = true, value = "select domain_name.* from domain_name, domain_name_reminder, reseller " +
"where domain_name.reminder_id = domain_name_reminder.id " +
"and domain_name_reminder.second_notification=current_date - interval ':totalDays days' " +
"and reseller.myCheck=true " +
"and reseller.id = domain_name.invoicing_party_id ")
public List<DomainName> findDomainsBySecondNotificationDate(@Param("totalDays")Integer totalDays);
关于java - Spring Data JPA 中的自定义(原始)查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516534/