use*_*206 12 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
}
Run Code Online (Sandbox Code Playgroud)
以下回购
public interface CustomerRepo extends Repository<CustomerModel, Long>{}
Run Code Online (Sandbox Code Playgroud)
我想做的是.检索给定日期的所有用户,例如(2013年8月1日加入的成员)但问题是我的数据库上的membershipDate有时间.如何忽略时间并检索给定日期的所有用户?
Oli*_*ohm 33
不幸的是,对于JodaTime来说,唯一的解决方法是使用Between关键字并使用两个DateTime实例组成一天.
interface CustomerRepo extends Repository<CustomerModel, Long>{
List<CustomerModel> findByMemberShipDateBetween(DateTime start, DateTime end);
}
Run Code Online (Sandbox Code Playgroud)
如果您的域模型Date在内部使用Java ,则可以使用此样式:
interface CustomerRepo extends Repository<CustomerModel, Long>{
List<CustomerModel> findByMemberShipDate(@Temporal(TemporalType.DATE) Date date);
}
Run Code Online (Sandbox Code Playgroud)
不是@Temporal注释是自定义的Spring Data JPA,因为普通的JPA目前不允许使用参数.Date不幸的是,这仅适用于Java 的原因是当前JPAPI的限制.该setParameter(…)方法Query只接受一个TemporalTypefor类型的参数Date.我们可以尝试在参数绑定上转换JodaTime对象,但我想持久性提供程序将拒绝由于类型不匹配(DateVS. DateTime).
您可以做一些解决方法:创建 DateTime dayBegin 和 DateTime dayEnd 例如 2013-07-04 00:00:00 和 2013-07-04 23:59:59 并通过查询获取所需的对象:
List<CustomerModel> findByMembershipBetween(DateTime dayBegin, DateTime dayEnd);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53871 次 |
| 最近记录: |