Java 使用 JPA/Hibernate Criteria 在日期之间进行拉取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4256561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JPA/Hibernate Criteria to pull between a date
提问by christophmccann
I am trying to using the following code to pull a list of Experience objects from a MySQL
table. Each experience has a from datetime
column and a to datetime
column and I only want to pull rows where todays date falls in between the from and to.
我正在尝试使用以下代码从MySQL
表中提取 Experience 对象列表。每个体验都有一个 fromdatetime
列和一个 todatetime
列,我只想拉出今天日期介于 from 和 to 之间的行。
I am using JPA 2.0 running off of Hibernate.
我正在使用运行 Hibernate 的 JPA 2.0。
Date currentDate = new Date();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Experience> query = builder.createQuery(Experience.class);
Root<Experience> root = query.from(Experience.class);
builder.between(currentDate, root.get("from"), root.get("to"));
return entityManager.createQuery(query).getResultList();
My issue is that builder.between()
obviously wont allow me to pass a Date object.
我的问题是builder.between()
显然不允许我传递 Date 对象。
Is there a better solution to my problem?
我的问题有更好的解决方案吗?
采纳答案by axtavt
You can pass it as a parameter:
您可以将其作为参数传递:
ParameterExpression<Date> d = builder.parameter(Date.class);
builder.between(d, root.<Date>get("from"), root.<Date>get("to"));
return entityManager.createQuery(query)
.setParameter(d, currentDate, TemporalType.DATE).getResultList();
Note that in this case you need to specify desired temporal type.
请注意,在这种情况下,您需要指定所需的时间类型。
Also you can rely on the database's current date: builder.currentDate()
, builder.currentTimestamp()
, etc
您也可以依靠数据库的当前日期:builder.currentDate()
,builder.currentTimestamp()
,等
回答by Patrick Farrell
This link looks promising: http://www.javalobby.org/articles/hibernatequery102/
这个链接看起来很有希望:http: //www.javalobby.org/articles/hibernatequery102/
You can use the criteria.ge("date", root.get("from"));
and criteria.le("date"), root.get("to"));
to create the between claus
您可以使用criteria.ge("date", root.get("from"));
andcriteria.le("date"), root.get("to"));
来创建 between 子句
If those aren't available, then you might need to look into writing HQL.
如果这些不可用,那么您可能需要考虑编写 HQL。
回答by Pedro Robles
You should switch your arguments:
你应该改变你的论点:
builder.between(root.<Date>get("dateColumn"), startDate, endDate);
回答by Bernie
You're just missing the call to CriteriaBuilder.literal():
您只是错过了对 CriteriaBuilder.literal() 的调用:
Date currentDate = new Date();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Experience> query = builder.createQuery(Experience.class);
Root<Experience> root = query.from(Experience.class);
builder.between(builder.literal(currentDate), root.get("from"), root.get("to"));
return entityManager.createQuery(query).getResultList();