[英]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?
I have a postgres database that has a table "draft"
containing a column set_up_time, saved as a timestamp: 我有一个postgres数据库,其中有一个表
"draft"
其中包含一列set_up_time,另存为时间戳:
@Column(nullable = false)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
LocalDateTime setUpTime;
For certain reasons I need to keep this structure as it is. 由于某些原因,我需要保持这种结构不变。 However I am expecting database queries
searching for records, for setUpTime as LocalDateTime and setUpTime as LocalDate
. 但是我期望数据库查询
searching for records, for setUpTime as LocalDateTime and setUpTime as LocalDate
。 How can I map it in a way, that hibernate queries the database with both these types for setUpTime, and returns the results accordingly? 我该如何以某种方式映射它,使休眠状态同时使用这两种类型的数据库查询setUpTime,并相应地返回结果?
The schema I am using is : 我正在使用的架构是:
"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))"
What you can do is write your queries like this: 您可以做的是这样编写查询:
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.