Spring数据查询日期时间只有日期 [英] Spring Data Querying DateTime with only Date

查看:17
本文介绍了Spring数据查询日期时间只有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据模型

public class CustomerModel{

  @Column
  @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
  private DateTime membershipDate;
  //Other properties and getters
}

还有下面的回购

public interface CustomerRepo  extends Repository<CustomerModel, Long>{}

我想做的是.检索给定日期的所有用户,例如(2013 年 8 月 1 日加入的成员),但问题是在我的数据库上,membershipDate 有时间.如何忽略时间并检索给定日期的所有用户?

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?

推荐答案

不幸的是,使用 JodaTime 的唯一方法是使用 Between 关键字并使用两个 DateTime 实例一天.

Unfortunately with JodaTime the only way around this is using the Between keyword and use two DateTime instances making up the day.

interface CustomerRepo extends Repository<CustomerModel, Long>{

  List<CustomerModel> findByMemberShipDateBetween(DateTime start, DateTime end);
}

如果您的域模型在内部使用 Java Date,您可以使用这种样式:

If your domain model used Java Dates internally you could've used this style:

interface CustomerRepo extends Repository<CustomerModel, Long>{

  List<CustomerModel> findByMemberShipDate(@Temporal(TemporalType.DATE) Date date);
}

@Temporal 注释不是自定义 Spring Data JPA 注释,因为当前不允许在参数上使用普通 JPA 注释.不幸的是,这只适用于 Java Date 的原因是当前 JPAPI 的限制.Query 上的 setParameter(…) 方法只为 Date 类型的参数采用 TemporalType.我们可以尝试在参数绑定上转换 JodaTime 对象,但我猜持久性提供者会因为类型不匹配而拒绝它(Date VS. DateTime).

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 Dates 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).

这篇关于Spring数据查询日期时间只有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆