Tuesday, 1 October 2013

HQL between two time interval

HQL between two time interval

How is the correct format to use a HQL between 2 intervals of date times.
I have this SQL in oracle:
select * from vw_diariorecauda where ts_crea between TO_DATE
('01/10/2013 00:00:00','dd/MM/yyyy hh24:mi:ss')
and TO_DATE ('01/10/2013 23:59:59', 'dd/MM/yyyy hh24:mi:ss')
My HQl:
**the field:**
private Date tsCrea;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TS_CREA")
public Date getTsCrea() {
return this.tsCrea;
}
**DAO:**
public List<VwDiariorecauda> buscarFecha(Date fecha) {
log.trace(fecha);
Calendar c = Calendar.getInstance();
c.setTime(fecha);
c.add(Calendar.HOUR, 0);
c.add(Calendar.MINUTE, 0);
c.add(Calendar.SECOND, 0);
Date fInicio = c.getTime();
c.add(Calendar.HOUR, 23);
c.add(Calendar.MINUTE, 59);
c.add(Calendar.SECOND, 59);
Date fFin = c.getTime();
Session session = sessionFactory.getCurrentSession();
Query query=session.createQuery("FROM VwDiariorecauda WHERE
tsCrea BETWEEN :hora1 AND
:hora2").setTimestamp("hora1",fInicio).
setTimestamp("hora2",fFin);
return query.list();
the hql works good but the results is different with the simple sql. How
can I improve this hql.
Thanks in advance.

No comments:

Post a Comment