[英]How to query JPA LocalDateTime field with a LocalDate value?
[英]How to specify a field, so that when we query, using Joda LocalDate or LocalDateTime value for the field, it is mapped to same column?
我有一个postgres数据库,其中有一个表"draft"
其中包含一列set_up_time,另存为时间戳:
@Column(nullable = false)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
LocalDateTime setUpTime;
由于某些原因,我需要保持这种结构不变。 但是我期望数据库查询searching for records, for setUpTime as LocalDateTime and setUpTime as LocalDate
。 我该如何以某种方式映射它,使休眠状态同时使用这两种类型的数据库查询setUpTime,并相应地返回结果?
我正在使用的架构是:
"create table draft (dtype varchar(31) not null, id int8 not null, creation_time timestamp not null, cust_ord varchar(1), description varchar(255), amount decimal(12, 2), user varchar(6), txn_time timestamp, text1from varchar(36), text2from varchar(36), text3from varchar(36), primary key (id))"
您可以做的是这样编写查询:
public void findDrafts(LocalDate targetDate) {
final Query query = entityManager.createQuery(
"from Draft where setUpTime => :beginOfDay and setUpTime < :beginOfNextDay");
query.setParameter("beginOfDay", targetDate.toDateTimeAtStartOfDay().toLocalDateTime());
query.setParameter("beginOfNextDay", targetDate.plusDays(1).toDateTimeAtStartOfDay().toLocalDateTime());
return query.getResultList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.