-->
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.  [ 2 posts ] 
Author Message
 Post subject: Data cast with criterias
PostPosted: Fri Nov 13, 2009 10:23 am 
Newbie

Joined: Fri Nov 13, 2009 9:39 am
Posts: 2
Hi guys!

I appreciate your help in solving a problem I faced:

A code is listed below:
superior_date => is an SQL DATE field
Code:

Property prop = Property.forName("superior_date");

DetachedCriteria crit = DetachedCriteria.forClass(Some.class);

String date = "11-11-2000";
crit.add(prop.like(date, MatchMode.ANYWHERE));



After running the code I get a ClassCastException -> String cannot be cast to java.util.Date;

I am interesting in comparing properties as simple Strings, because sometimes user can enter a part of a date. Therefore I need to convert java.util.Date to String with dd-mm-yyyy format.

How can I affect the generated query and cast target java.util.Date to String?

In HQL there is a CAST function, but HQL is not acceptable in my case, only Criterias are allowed.

Thank you!


Top
 Profile  
 
 Post subject: Re: Data cast with criterias
PostPosted: Fri Nov 13, 2009 11:15 am 
Newbie

Joined: Fri Nov 13, 2009 9:39 am
Posts: 2
I found out that there could be an SQL restriction, but it can be written using native SQL only...

Code:
crit.add(Expression.sqlRestriction("TO_CHAR("superior_date", 'dd-mm-yyyy') like '%" + date + "%'"));


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