使用 JPA 比较两个日期
我需要比较 JPQL 查询中的两个日期,但它不起作用。
这是我的查询:
Query query = em.createQuery(
"SELECT h
FROM PositionHistoric h,
SeoDate d
WHERE h.primaryKey.siteDb = :site
AND h.primaryKey.engineDb = :engine
AND h.primaryKey.keywordDb = :keyword
AND h.date = d
AND d.date <= :date
ORDER BY h.date DESC");`
我的参数日期是 java.util.Date
我的查询返回一个对象列表,但日期是我的参数的上限和下限。
有人知道该怎么做吗?
谢谢。
I need to compare two dates in a JPQL query but it doesn't work.
Here is my query:
Query query = em.createQuery(
"SELECT h
FROM PositionHistoric h,
SeoDate d
WHERE h.primaryKey.siteDb = :site
AND h.primaryKey.engineDb = :engine
AND h.primaryKey.keywordDb = :keyword
AND h.date = d
AND d.date <= :date
ORDER BY h.date DESC");`
My parameter date is a java.util.Date
My query return a objects list but the dates are upper and lower to my parameter.
Someone kown how to do this ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用的查询是:
请注意,按
h.date
排序是毫无意义的(因为它指向 SeoDate 实体,因此实际上您是按 PK 排序)并且您不能按 < code>h.date.date 因为 SeoDate 映射为多对一
。另请注意,
INNER JOIN FETCH h.date
行本身并不是必需的,但如果您实际上需要在返回的列表中使用 SeoDate 属性,它将为您节省一些额外的惰性查询开销。The query you should use is:
Note that ordering by
h.date
is rather pointless (as it points to SeoDate entity and thus in effect you're ordering by PK) and you can't order byh.date.date
since SeoDate is mapped asmany-to-one
.Also note that
INNER JOIN FETCH h.date
line is not necessary per se but it will save you some overhead on additional lazy queries IF you will actually need to use SeoDate attributes in the returned list.我建议使用 java.sql.Date 而不是 java.util.Date
因为你将它传递给 JPQL(一种 SQL)。
我通常使用 TIMESTAMP 而不是 Date,但如果你没有选择......
是不是: h.date = d.date 而不是 h.date = d ?
I suggest to use java.sql.Date instead of java.util.Date
Because you pass it to a JPQL(kind of SQL).
I usually use TIMESTAMP instead of Date, but if you didn't have the choice..
And is it : h.date = d.date instead h.date = d ?