简体   繁体   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?

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.

相关问题 如何使用 LocalDate 值查询 JPA LocalDateTime 字段? - How to query JPA LocalDateTime field with a LocalDate value? 如何使用 LocalDate 查询 LocalDateTime? - How to query LocalDateTime with LocalDate? 来自 LocalDate 和字符串的 Joda LocalDateTime - Joda LocalDateTime from LocalDate and string 我如何使用 REST controller GET 将 LocalDateTime 反序列化为 LocalDate,其中响应值为 LocalDateTime,DTO 中的值为 LocalDate? - How i can deserialize LocalDateTime into LocalDate using a REST controller GET where value in response is LocalDateTime, value in DTO is LocalDate? 如何使用DataNucleus查询/保留joda-time DateTime字段 - How to query/persist joda-time DateTime field using DataNucleus 如何在 JPA 查询中将 LocalDateTime 转换为 LocalDate? - How to convert LocalDateTime to LocalDate in JPA Query? 从LocalDateTime字符串到LocalDate的Joda时间 - Joda-time from LocalDateTime string to LocalDate Spring JPA 实体映射 LocalDate 和 LocalDateTime 字段到 Sql 服务器 - Spring JPA entity mapping LocalDate and LocalDateTime field to Sql Server 使用NamedParamterJdbcTemplate对象指定要查询的mysql字段和值 - Using a NamedParamterJdbcTemplate object to specify a mysql field and value to query on 如何在休眠查询中使用映射字段? - How to use mapped field in hibernate query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM