-->
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: Criteria and Date data type
PostPosted: Mon Jul 02, 2007 11:18 am 
Newbie

Joined: Thu Feb 22, 2007 6:15 am
Posts: 7
Hello,

I develop an application to build automatically request.
In one of my tests, I want to put a date constraint in a query like
select x from t where d='30/04/2006'

input : '30/04/2006'

I want to build a criteria on it and this is done :
Code:
criteria.add(Restrictions.ilike("sample.entry_date", createNewInstance("Sample", "entry_date", "30/04/2006")));


Since the entry_date field of Class Sample is java.util.Date, the method 'createNewInstance' returns a new java.util.Date with the String parameter "30/04/2006".
But there is an exception : org.hibernate.type.DateType.toString

I think it's because the parameter is as formated as in my database : yyyy-MM-dd.

My question is simply how to integrate in a criteria a comparison between a field util.Date and a String parameter containing the date ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 3:26 pm 
Beginner
Beginner

Joined: Fri Apr 13, 2007 9:27 am
Posts: 33
You need to pass a Date type object


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 4:23 am 
Newbie

Joined: Thu Feb 22, 2007 6:15 am
Posts: 7
This is what I did !

I create a method called 'createNewInstance' which return an Object, used in criteria restriction:
Code:
criteria.add(Restrictions.ilike("sample.entry_date", createNewInstance("Sample", "entry_date", "30/04/2006")));


createNewInstance method takes 3 String parameters : the first and the second are the class name and the field name; I retrieve the corresponding data type and I build and return a new instance of this data type object with the third String parameter.
In that way, I have the good instancied object to put in the restriction methods ! With the example I gave below, createNewInstance returns a new java.util.Date with 30/04/2006.

Since yesterday, I had problems with all Restrictions methods like ilike, gt, lt...
Today, I carefully did my test and actually, only the ilike method doesn't work !!! the same code, but not the method name !!! ilike method doesn't work and gt does ???
Code:
criteria.add(Restrictions.ilike("sample.entry_date", createNewInstance("Sample", "entry_date", "30/04/2006")));
criteria.add(Restrictions.gt("sample.entry_date", createNewInstance("Sample", "entry_date", "30/04/2006")));


Any idea ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 8:07 am 
Beginner
Beginner

Joined: Fri Apr 13, 2007 9:27 am
Posts: 33
funkadeclic wrote:
This is what I did !

Code:
criteria.add(Restrictions.ilike("sample.entry_date",



What are you trying to do using 'ilike' to compare dates? ilike is used mainly to compare strings. A date is a number. It is either equal, less than, or greater than another date/number.


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.