Java Spring数据查询只有日期的DateTime
我有这个数据模型Java Spring数据查询只有日期的DateTime,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,我有这个数据模型 public class CustomerModel{ @Column @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime") private DateTime membershipDate; //Other properties and getters } 以及以下回购协议 public interface CustomerRepo extends Repository<Cu
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>{}
public接口CustomerRepo扩展存储库{}
我想做的是。检索给定日期的所有用户,例如(2013年8月1日加入的成员),但问题是在my DB上,membershipDate有一个时间。如何忽略时间并检索给定日期的所有用户 不幸的是,对于JodaTime,唯一的解决方法是使用
Between
关键字并使用两个DateTime
实例组成一天
interface CustomerRepo extends Repository<CustomerModel, Long>{
List<CustomerModel> findByMemberShipDateBetween(DateTime start, DateTime end);
}
不是,
@Temporal
注释是一个定制的Spring数据JPA注释,因为参数上当前不允许使用普通JPA注释。不幸的是,这只适用于JavaDate
s的原因是当前jpapi的局限性。Query
上的setParameter(…)
方法只对Date
类型的参数采用TemporalType
。我们可以尝试在参数绑定上转换JodaTime对象,但我想持久性提供程序会拒绝,因为类型不匹配(Date
VS.DateTime
).您可以做一些变通:创建DateTime dayBegin和DateTime dayEnd,例如2013-07-04 00:00:00和2013-07-04 23:59:59,并通过查询获取所需的对象:
List<CustomerModel> findByMembershipBetween(DateTime dayBegin, DateTime dayEnd);
List findbymembershipBeween(DateTime dayBegin,DateTime dayEnd);
jODA和spring&JPA实际上是好朋友,请访问:
在spring数据的v1.6.2中,此注释仅支持java.util.Date类型的参数。因此,此解决方案目前不适用于比较DateTime对象。找到了当前的javadocs。@yurodivuie-这是一个很好的捕获。谢谢你的指点。我更新了答案,为JodaTime提供了一个替代解决方案,并概述了为什么目前只能使用Java
Date
类型。实体中的Java 8 ZonedDateTime和LocalDate作为搜索参数如何?流是否将结果带回内存或保持光标打开。我这样问是因为查询了大量的结果集,然后映射。谢谢目前可用的实现都使用游标。我建议打开一个新问题,以防你想知道细节,因为我们在这里有点偏离当前的问题。
List<CustomerModel> findByMembershipBetween(DateTime dayBegin, DateTime dayEnd);