2015-09-11 50 views
1

我使用自定義查詢在JPA,它不會讓我用interval關鍵字。如果我在查詢中不使用- interval '7 days',它會給出正確的輸出。自定義查詢在JPA Spring MVC中

異常說: 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天通知第二日的記錄。作爲

public interface DomainNameRepository extends JpaRepository<DomainName, Long>, 
QueryDslPredicateExecutor<DomainName> { 

我的查詢是給在pgadmin postgresql client正確的輸出

我的接口中聲明,我很驚訝爲什麼我不能在這裏使用的關鍵詞。

+0

該解決方案可以幫助您的問題。 http://stackoverflow.com/questions/1945615/how-to-map-the-type-in​​terval-in-hibernate#answer-6139581 – javasenior

+1

JPQL和SQL是兩種不同的語言。試圖在postgresql客戶端中使用JPQL查詢,反之亦然沒有多大意義。 –

+0

您是否嘗試將'nativeQuery = true'添加到@Query註釋中? – reto

回答

4

這解決了我的問題。

我用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);