![](/img/trans.png)
[英]JPA method not returning results even though the generated query returns results when used in postgres console
[英]Spring Data/Hibernate native query returning null even though real query returns results
所以我試圖使用mysql datediff函數來獲得發票舊的平均天數。
我的查詢如下所示:
select avg(datediff(coalesce(nullableDateTimeColumn, now()), i.createdTimeStamp)) from invoice i
where i.createdTimestamp >= DATE_VARIABLE_HERE and (invoiceStatus = 'TEXT_HERE' or nullableDateTimeColumn is not null)
我的實際spring數據repo定義如下所示:
@Repository
interface InternalInvoiceRepository extends PagingAndSortingRepository<Invoice, Long> {
@Query(value = "select avg(datediff(coalesce(nullableDateTimeColumn, now()), i.createdTimeStamp)) from invoice i " +
"where i.createdTimestamp >= ?1 " +
"and (invoiceStatus = 'TEXT_HERE' or nullableDateTimeColumn is not null)", nativeQuery = true)
BigDecimal getAverageTimeInvoiceEntryTimeForInvoicesNewerThan(LocalDateTime time);
}
問題是結果總是為null,我嘗試將返回類型設置為double但這也不起作用。 如果我將log4j.logger.org.hibernate.SQL
設置為DEBUG
並查看sql sql是否正確。 如果我運行sql我得到一個數字,所以我知道hibernate運行的sql實際上是返回一個結果。 返回的值非常小,通常在0-5之間,所以它不是一些奇怪的溢出錯誤或其他類似的東西。 這似乎是一個簡單的場景,所以我有點困惑為什么這不起作用。
版本:
Spring data: 1.9.2.RELEASE
Spring: 4.2.4.RELEASE
Hibernate: 5.0.7.Final
hibernate-jpa-2.1-api: 1.0.0.Final
這適用於Oracle。 它可能會幫助你。
SELECT avg(TO_NUMBER(TO_CHAR(ADD_MONTHS(
(coalesce(nullableDateTimeColumn, SYSDATE)), TO_CHAR(i.createdTimeStamp, MM')), 'YYYYMMDD')))
FROM invoice i
WHERE i.createdTimeStamp >= '20160401' -- for example
AND ( invoiceStatus = 'TEXT_HERE'
OR nullableDateTimeColumn IS NOT NULL )
參考這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.