Hibernate标准获取currentDate>的所有行startDate和<结束日期

时间:2017-01-17 10:50:41

标签: java hibernate date criteria

我需要获取当前日期包含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

我需要在表格中获得2行0和4 ID

@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();
    }

返回0行

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

1 个答案:

答案 0 :(得分:2)

在条件允许的情况下,dateStart >= currentDate and dateEnd <= currentDate

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

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