Java Spring Data查询DateTime只有日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18082276/
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
Spring Data Querying DateTime with only Date
提问by user962206
I have this Data Model
我有这个数据模型
public class CustomerModel{
@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime membershipDate;
//Other properties and getters
}
And the following repo
以及以下回购
public interface CustomerRepo extends Repository<CustomerModel, Long>{}
What I want to do is. Retrieve all users on a given date eg(Members that Joined in August 1 2013) however the problem is that on my DB the membershipDate has a time with it. how can I ignore the time and retrieve all users on a given date?
我想做的是。检索给定日期的所有用户,例如(2013 年 8 月 1 日加入的成员),但问题是在我的数据库上,membershipDate 有时间。如何忽略时间并检索给定日期的所有用户?
采纳答案by Oliver Drotbohm
Unfortunately with JodaTime the only way around this is using the Between
keyword and use two DateTime
instances making up the day.
不幸的是,对于 JodaTime,解决这个问题的唯一方法是使用Between
关键字并使用两个DateTime
实例构成一天。
interface CustomerRepo extends Repository<CustomerModel, Long>{
List<CustomerModel> findByMemberShipDateBetween(DateTime start, DateTime end);
}
If your domain model used Java Date
s internally you could've used this style:
如果您的域模型Date
在内部使用 Java s,您可以使用这种风格:
interface CustomerRepo extends Repository<CustomerModel, Long>{
List<CustomerModel> findByMemberShipDate(@Temporal(TemporalType.DATE) Date date);
}
Not the @Temporal
annotation is a custom Spring Data JPA one as the plain JPA one is currently not allowed on parameters. The reason that this only works with Java Date
s unfortunately is a limitation of the current JPAPIs. The setParameter(…)
method on Query
only takes a TemporalType
for parameters of type Date
. We could try converting the JodaTime objects on parameter binding but I guess the persistence providers will reject that due to the type mismatch then (Date
VS. DateTime
).
不是@Temporal
注释是自定义的 Spring Data JPA 注释,因为当前不允许在参数上使用普通的 JPA 注释。Date
不幸的是,这只适用于 Java的原因是当前 JPAPI 的限制。的setParameter(…)
上方法Query
只需要TemporalType
为参数类型为Date
。我们可以尝试在参数绑定上转换 JodaTime 对象,但我猜持久性提供者会因为类型不匹配而拒绝它(Date
VS. DateTime
)。
回答by yname
You can do some workaround: create DateTime dayBegin and DateTime dayEnd which are e.g. 2013-07-04 00:00:00 and 2013-07-04 23:59:59 and fetch needed objects by query:
您可以做一些解决方法:创建 DateTime dayBegin 和 DateTime dayEnd 例如 2013-07-04 00:00:00 和 2013-07-04 23:59:59 并通过查询获取所需的对象:
List<CustomerModel> findByMembershipBetween(DateTime dayBegin, DateTime dayEnd);
回答by Robocide
jODA and spring & JPA actually good friends, just take alook at :
jODA 和 spring & JPA 其实是好朋友,看看:
http://blog.netgloo.com/2015/04/06/spring-boot-using-joda-time-on-jpa-entity-with-hibernate/
http://blog.netgloo.com/2015/04/06/spring-boot-using-joda-time-on-jpa-entity-with-hibernate/
enjoy
请享用