简体   繁体   中英

Using dates in Criteria Query where clause

I have an entity with a java.util.Date field stored as a 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.

The setup is an embedded H2-database in Spring with Hibernate. I've tried using PostgreSQL instead of H2 and it works. I've also tried to set H2 in PostgreSQL-mode, but it doesn't change anything.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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