I have an hql problem running a query to compare dates ignoring time. When I run the following query:
"from Event where date(eventTime)=?" I unexpectedly get a bunch of updates. (see
The generated SQL)If I just compare the dates directly like:
"from Event where eventTime=?" I dont get the update problem but I dont get any results because the date im using has a different time. How can I compare just the date portion of 2 different dates using hql without generating a dirty check update?
Hibernate version: 3
Mapping documents: Event.hbm.xml
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class mutable="true" proxy="com.galleryweb.model.event.Event" table="events" name="com.galleryweb.model.event.Event">
<id column="id" unsaved-value="-1" name="id">
<generator class="native"/>
</id>
<property name="title" not-null="false" column="title" unique="false"/>
<property name="location" not-null="false" column="location" unique="false"/>
<property name="locale" not-null="false" column="locale" unique="false"/>
<property name="description" not-null="false" type="text" column="description" unique="false"/>
<property name="labels" not-null="false" column="labels" unique="false"/>
<property name="visibility" not-null="true" column="visibility" unique="false"/>
<property name="rating" not-null="false" column="rating" unique="false"/>
<property name="views" not-null="false" column="views" unique="false"/>
<property name="eventTime" not-null="true" column="event_time" unique="false"/>
<many-to-one column="client_id" name="client" class="com.galleryweb.model.client.Client"/>
<many-to-one column="icon_id" cascade="all" name="icon" class="com.galleryweb.model.display.Icon"/>
<many-to-one column="schedule_id" cascade="all" name="schedule" class="com.galleryweb.model.event.Schedule"/>
<set inverse="true" sort="natural" cascade="all,delete-orphan" name="eventComments">
<key column="event_id"/>
<one-to-many class="com.galleryweb.model.comment.EventComment"/>
</set>
<property name="theme" not-null="false" column="theme_id" unique="false"/>
<set table="positions" inverse="true" cascade="all,delete-orphan" name="positions">
<key column="model_id"/>
<one-to-many class="com.galleryweb.model.display.Position"/>
</set>
<property name="domain" not-null="true" column="domain" unique="false"/>
<property name="username" not-null="true" column="username"/>
<property name="isMature" not-null="true" type="yes_no" column="is_mature"/>
<property name="isIndexUpdated" not-null="true" type="yes_no" column="is_index_updated"/>
<property name="createTime" not-null="true" column="create_time" unique="false"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Object[] terms = {date, domain};
String query = "from Event where date(eventTime)=? and domain=? order by createTime desc";
List models;
try {
models = getHibernateTemplate().find(query, terms);
} catch (Exception e) {
throw new DataRetrievalException("problem finding models for query " + query, e);
}
return models;
Name and version of the database you are using: MySQL 5 (locally for now)
The generated SQL (show_sql=true):Code:
DEBUG com.galleryweb.service.impl.EventServiceImpl (EventServiceImpl.java:316) - EventServiceImpl.createEventDay
DEBUG EventDAOImpl - EventDAOImpl.getEventsByDateAndDomain
Hibernate: update events set title=?, location=?, locale=?, description=?, labels=?, visibility=?, rating=?, views=?, event_time=?, client_id=?, icon_id=?, schedule_id=?, theme_id=?, domain=?, username=?, is_mature=?, is_index_updated=?, create_time=? where id=?
Hibernate: update events set title=?, location=?, locale=?, description=?, labels=?, visibility=?, rating=?, views=?, event_time=?, client_id=?, icon_id=?, schedule_id=?, theme_id=?, domain=?, username=?, is_mature=?, is_index_updated=?, create_time=? where id=?
Hibernate: update events set title=?, location=?, locale=?, description=?, labels=?, visibility=?, rating=?, views=?, event_time=?, client_id=?, icon_id=?, schedule_id=?, theme_id=?, domain=?, username=?, is_mature=?, is_index_updated=?, create_time=? where id=?
.
.
.