Hi, Im having a problem when I use Criteria with a between restriction with Calendars as parameters.
I have a class Reserva with 2 attributes: "desde" and "hasta" wichs are Calendar type
Basically, is a class that that represents a reservation in a hotel, with dates.
I need to know if a date of the reservation is between to dates sent has parameter of the method. When I use getReservas() (listed below) I get an empty list! I dont know why! Because when I make the sql query with the same parameters always have a result.
I´m using the Spring framework as you can see below.
I already check that the parameters are sent ok. I make a .toString() of the criteria and is ok I think.
When I tried to pass hasta parameters a format string as date (for example: 2008-07-31) error is display that the casting to Calendar can be made, so I think is Ok that a calendar parameter is sent.
Someone knows how to work with gregorianCalendar in a between restrictions of Criteria??
Hibernate version:3.2.5
Mapping documents:
Reserva.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="modelo.hotel.Reserva" table="RESERVAS">
<id name="id" type="long" unsaved-value="-1">
<generator class="identity"/>
</id>
<many-to-one name="cliente" column="idCliente" class="modelo.hotel.Cliente"/>
<many-to-one name="habitacion" column="idHabitacion" class="modelo.hotel.Habitacion"/>
<property name="desde" type="calendar_date"/>
<property name="hasta" type="calendar_date"/>
<property name="confirmacion" type="boolean"/>
</class>
</hibernate-mapping>
Code:
public List<Habitacion> getReservas(GregorianCalendar unaFechaDesde, GregorianCalendar unaFechaHasta) {
DetachedCriteria subquery = DetachedCriteria.forClass(modelo.hotel.Reserva.class);
Criterion criterio1 = Restrictions.between("desde", unaFechaDesde, unaFechaHasta);
subquery.add(criterio1);
subquery.setProjection(Property.forName("habitacion"));
List lstSubquery = getHibernateTemplate().findByCriteria(subquery);
return lstSubquery;
}
Name and version of the database you are using:MySql 5.0
The generated SQL (show_sql=true):
Hibernate: select this_.idHabitacion as y0_ from RESERVAS this_ where this_.desde between ? and ?
This is the SQL query I executed and have result:
Select idHabitacion from Reservas where desde between ´2008-07-20´and ´2008-07-31´
Obviously the GregorianCalendars that are sent to the method have the same values.