[英]Spring Data/Hibernate native query returning null even though real query returns results
So I'm trying to use the mysql datediff functions to get an average of number of days that an invoice is old. 所以我试图使用mysql datediff函数来获得发票旧的平均天数。
My query looks like this: 我的查询如下所示:
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)
and my actual spring data repo definition looks like this: 我的实际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);
}
The problem is that the result is always null, I tried setting the return type to double but that also didn't work. 问题是结果总是为null,我尝试将返回类型设置为double但这也不起作用。 If I set
log4j.logger.org.hibernate.SQL
to DEBUG
and look at the sql the sql is correct. 如果我将
log4j.logger.org.hibernate.SQL
设置为DEBUG
并查看sql sql是否正确。 If I run the sql I get a number back, so I know the sql that is being run by hibernate is actually returning a result. 如果我运行sql我得到一个数字,所以我知道hibernate运行的sql实际上是返回一个结果。 The value being return is pretty small, normally between 0-5, so it's not some strange overflow error or anything else like that.
返回的值非常小,通常在0-5之间,所以它不是一些奇怪的溢出错误或其他类似的东西。 This seems like a simple scenario, so I'm a little perplexed as to why this isn't working.
这似乎是一个简单的场景,所以我有点困惑为什么这不起作用。
Versions: 版本:
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
This is for Oracle. 这适用于Oracle。 It may helps you.
它可能会帮助你。
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.