簡體   English   中英

如何指定一個字段,以便在查詢時使用字段的Joda LocalDate或LocalDateTime值將其映射到同一列?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM