java - 如何指定一个字段,以便当我们查询时,使用该字段的 Joda LocalDate 或 LocalDateTime 值,将其映射到同一列?

标签 java database hibernate hibernate-mapping

我有一个 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/

相关文章:

java - 在 Eclipse 中运行 AlgoTrader

java - 双击时,jtable 数据将替换为之前的结果

java - 无法访问或导航 jsp 代码中的图像文件

python - Hadoop:在 Ubuntu 12.04 中通过 NameNode 格式化 HDFS 文件系统

java - Hibernate - 如何检索已为 L2 缓存配置的所有实体类的列表

AngularJS $resource 和 JAX-RS

ubuntu 14.04中sqldeveloper的java路径

php - 师生数据库设计

mysql - 设计关系型数据库,有一种若隐若现的厄运感

java - 如何将hibernate映射到JSP View ?