我有一个 postgres 数据库,其中有一个表“draft”
,其中包含 set_up_time 列,保存为时间戳:
@Column(nullable = false)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
LocalDateTime setUpTime;
由于某些原因,我需要保持这个结构不变。但是,我期望数据库查询搜索记录,将 setUpTime 作为 LocalDateTime 并将 setUpTime 作为 LocalDate
。我如何以某种方式映射它,让 hibernate 使用这两种类型的 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();
}
关于java - 如何指定一个字段,以便当我们查询时,使用该字段的 Joda LocalDate 或 LocalDateTime 值,将其映射到同一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16666874/