-->
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.  [ 1 post ] 
Author Message
 Post subject: Qerying a date field just by using the year
PostPosted: Tue Mar 29, 2005 5:35 pm 
Newbie

Joined: Sun Mar 27, 2005 1:22 pm
Posts: 15
Hibernate 3.0

Hi,

Sorry if this is an obvious question. i just can't find the answer anywhere.

I am trying to query a database table on a date field column. That works ok if i put in all of the data in the date but how can i set the query to return all the dates for a year for exaple? How can i 'wildcard' a field.

I have a field in a POJO containg a date that i set with this code..

Code:
session = this.getHibernateSession() ;
Transaction tx = session.beginTransaction() ;
         
CalendarEvent  ce = new CalendarEvent(  ) ;

// ORGANISE DATE
calendar = Calendar.getInstance() ;
calendar.clear() ;
calendar.set( Calendar.YEAR , 2005 ) ;         
calendar.set( Calendar.MONTH  , new Integer( month ).intValue() ) ;
calendar.set( Calendar.DATE  , new Integer( day ).intValue() ) ;

// SET THE VALUES
ce.setShortDesc( shortDesc ) ;
ce.setLongDesc( longDesc ) ;         
ce.setDate( calendar ) ;

session.save( ce ) ;         
tx.commit();   


That works OK.

I can recall the objects, CalendarEvent, OK by searching on the datefield like so...

Code:
session = this.getHibernateSession() ;

Query query = session.createQuery( "select ce from CalendarEvent ce where ce.date = :date" ) ;

Calendar calendar = Calendar.getInstance();
calendar.clear() ;
calendar.set( Calendar.YEAR , 2005 ) ;         
calendar.set( Calendar.MONTH  , new Integer( month ).intValue() ) ;
calendar.set( Calendar.DATE  , new Integer( day ).intValue() ) ;

query.setParameter( "date" , calendar  ) ;



but i cannot seem to just enter the year on a query and get all entries for that year. i.e. removing the

Code:
calendar.set( Calendar.MONTH  , new Integer( month ).intValue() ) ;
calendar.set( Calendar.DATE  , new Integer( day ).intValue() ) ;
lines

can you help.

Thanks,

David.


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

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.