简体   繁体   English

在Criteria Query where子句中使用日期

[英]Using dates in Criteria Query where clause

I have an entity with a java.util.Date field stored as a TemporalType.DATE . 我有一个实体,其java.util.Date字段存储为TemporalType.DATE When passing a java.util.Date with hours, minutes or seconds to the where clause of a criteria query I can't seem to get a match from the database. 将带有小时,分钟或秒的java.util.Date传递给条件查询的where子句时,我似乎无法从数据库中获得匹配。

The setup is an embedded H2-database in Spring with Hibernate. 该设置是Spring with Hibernate中的嵌入式H2数据库。 I've tried using PostgreSQL instead of H2 and it works. 我尝试使用PostgreSQL而不是H2,它可以工作。 I've also tried to set H2 in PostgreSQL-mode, but it doesn't change anything. 我也尝试在PostgreSQL模式下设置H2,但它不会改变任何东西。

Given the entity 鉴于实体

@Entity
public class SomeEntity {

    @Id
    private int id;

    @Temporal(TemporalType.DATE)
    private java.util.Date aDate;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDate() {
        return aDate;
    }

    public void setDate(Date aDate) {
        this.aDate = aDate;
    }
}

The following query only returns a match if the hours, minutes and seconds of the parameter have been set to 0. 如果参数的小时,分​​钟和秒设置为0,则以下查询仅返回匹配项。

public List<SomeEntity> someQueryOnDate(Date date) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SomeEntity> query = cb.createQuery(SomeEntity.class);

    Root<SomeEntity> root = query.from(SomeEntity.class);
    Predicate dateEquals = cb.equal(root.get(SomeEntity_.date), date);
    query.where(dateEquals);

    // This list is always empty if the date in the predicate has a time part
    return em.createQuery(query).getResultList(); 
}

A full example follows. 完整的例子如下。 The test fails on the last assertion, where I query the database using a Date with hours, minutes and seconds set. 最后一个断言测试失败,我使用设置小时,分钟和秒的日期查询数据库。

@Test
public void testDateEquals() throws ParseException {
    Date dateWithoutTime = new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-03");
    Date dateWithTime    = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-07-03 09:45:01");

    createEntity(dateWithoutTime);

    List<SomeEntity> entitiesMatchingDateWithTime    = listAllEntitiesWithDate(dateWithTime);
    List<SomeEntity> entitiesMatchingDateWithoutTime = listAllEntitiesWithDate(dateWithoutTime);

    Assert.assertFalse("No entities matched the date without time", entitiesMatchingDateWithoutTime.isEmpty());
    Assert.assertFalse("No entities matched the date with time"   , entitiesMatchingDateWithTime.isEmpty());
}

private void createEntity(Date d) {
    SomeEntity entity = new SomeEntity();
    entity.setDate(d);
    em.persist(entity);

    // For good measure
    em.flush();
    em.clear();
}

private List<SomeEntity> listAllEntitiesWithDate(Date date) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SomeEntity> query = cb.createQuery(SomeEntity.class);

    Root<SomeEntity> root = query.from(SomeEntity.class);
    Predicate dateEquals = cb.equal(root.get(SomeEntity_.date), date);
    query.where(dateEquals);

    return em.createQuery(query).getResultList();
}

Maybe the solution is provided here: 也许解决方案在这里提供:

Hibernate Criteria for Dates 日期的休眠标准

You can use Restrictions to compare dates. 您可以使用Restrictions来比较日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM