如何指定一个字段,这样当我们查询时,使用Joda LocalDate或LocalDateTime值为字段,它是否映射到同一列?

时间:2013-05-21 09:43:00

标签: java database hibernate hibernate-mapping

我有一个postgres数据库,其中包含一个包含列set_up_time的表"draft",保存为时间戳:

@Column(nullable = false)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
LocalDateTime setUpTime;

出于某些原因,我需要保持这种结构不变。但是我期待数据库查询searching for records, for setUpTime as LocalDateTime and setUpTime as 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))"

1 个答案:

答案 0 :(得分:0)

您可以做的就是写下这样的查询:

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();
}