-->
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.  [ 4 posts ] 
Author Message
 Post subject: Dates and like clause
PostPosted: Wed May 02, 2007 5:17 am 
Newbie

Joined: Mon Mar 26, 2007 5:26 am
Posts: 10
Hibernate version:3.2

Hey all

I have a Movie class mapped with a "created" Timestamp property. How can I search for movies where the Timestamp starts with a certain date? I am receiving dates in the form of yyyy-mm-dd.

This is my code so far, which won't return any results at all.

Code:
@SuppressWarnings("unchecked")
   public List<Movie> findMoviesByDate(Date date, Integer noOfResults)
   {
      getHibernateTemplate().setMaxResults(noOfResults);
      DetachedCriteria criteria;
      criteria = DetachedCriteria.forClass(Movie.class);
      criteria.add(Restrictions.like("created", date));
      criteria.addOrder(Property.forName("created").desc());
      
      return getHibernateTemplate().findByCriteria(criteria);
      
   }


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 5:29 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
like this:

Code:
add(Restrictions.gt("created". new Date()))


this code fragment will select entities with "created" property greater than (gt) today (new Date())

See _Restrictions_ class for similar methods (le, between, ...)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 6:05 am 
Newbie

Joined: Mon Mar 26, 2007 5:26 am
Posts: 10
But I want it to be an equal date, not greater than or less than a certain date. The problem is that the datatype i am querying for is a Timestamp while I want all Timestamps within the range of a day (yyyy-mm-dd). The Restrictions.eq obviously won't work since there is no perfect match - can I somehow trim the created column before I query for it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 02, 2007 6:22 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
what precision you want? a day as the smallest time unit?

1. you have a representation of that Date in Java as a java.util.Date i guess? If so you, i do something like:

* i take that Date and create two new Dates -- the beginnig of a day and end of a day. simple if you use the java.util.Calendar

* use Restriction.between or combination of Restrictions.le || Restrictions.gt and make input both of the newly created dates as a interval -- the query generated contains st like: whete ? < dateProperty and dateProperty < ? -- you choose anything that lies between the interval boundaries

2. you may create your column as a date (to store only yyyy-mm-dd value) -- in that case any java.util.Date will be matched only by the fields it has ignoring any "lesser" time units (like hours and minutes). Use st like:

Code:
    <property name="validDate" type="date">
      <column name="valid_date" not-null="true"/>
    </property>


3. JFYI -- the "yyyy-mm-dd" is output of java.sql.Date, look for the db column type definition -- it could be only date and or date and time, you can't recognize it from this string output


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.