Hibernate 条件获取所有行,其中 currentDate > startDate 和 < endDate

Hibernate criteria get all rows where currentDate > startDate and < endDate

我需要获取当前日期包含 beetwen dateStart 和 dateAnd 的所有行。

如果当前日期为 2017 年 1 月 17 日并且

id  dateStart     dateEnd
0   01 JAN 2017   18 JAN 2017
1   01 JAN 2017   16 JAN 2017
2   18 JAN 2017   03 FEB 2017
4   17 JAN 2017   16 JAN 2017

我需要在 table

中获取 ID 为 0 和 4 的 2 行
@Override
    public List<Raffle> findCurrentRaffle() {
        Criteria criteria = getSession().createCriteria(Raffle.class);
        criteria.add(Restrictions.ge("dateStart",  new Date()));
        criteria.add(Restrictions.le("dateEnd", new Date()));
        return criteria.list();
    }

return 0 行

private Long id;
    @Column(name = "dateStart")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateStart;
    @Column(name = "dateEnd")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateEnd;

在你的条件下你有dateStart >= currentDate and dateEnd <= currentDate

要解决问题,只需将条件更改为dateStart <= currentDate and dateEnd >= currentDate

criteria.add(Restrictions.le("dateStart",  new Date()));
criteria.add(Restrictions.ge("dateEnd", new Date()));