-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: problem querying date only portion with hql
PostPosted: Mon Mar 26, 2007 8:30 pm 
Newbie

Joined: Thu Nov 02, 2006 11:38 am
Posts: 2
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=?
.
.
.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 3:19 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
You could change the FlushMode setting. I'm not sure if you can do this directly before and after a query or whether you need to do it at the start of a session, but this give you control over changes to the database.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 7:07 pm 
Newbie

Joined: Thu Nov 02, 2006 11:38 am
Posts: 2
That sounds like an interesting idea but I would prefer not to do that since I have lots of queries that I only want to compare dates for and I am using a filter which opens the session for the view. Do you know of a way to change the query method or syntax to make this work?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.