-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use TO_DATE SQl function with Criteria API
PostPosted: Wed Jun 11, 2008 4:30 pm 
Newbie

Joined: Wed Jun 11, 2008 4:15 pm
Posts: 2
Location: Dallas
Query:

select SALARY
from ACCOUNTS
where TO_DATE(SALARY_DATE, 'MM/DD/YYYY') = :salDate

Note:SALARY_DATE is a timestamp field in DB2

Some one help me, how to build this query in Hibernate Criteria API.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 9:33 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, there's a few ways to go. Of course, the Criteria API is object based, so much depends on how many objects you have.

This might easily be accomplished by creating an Accounts objects an using a greater than or less than Restriction on the salary range or date range, or whatever property you want to place a restriction on.

Here's a quick but very good tutorial on using the Criteria API. There are some simple examples that should give you enough ideas on how to customize a solution for your code:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 1:36 pm 
Newbie

Joined: Wed Jun 11, 2008 4:15 pm
Posts: 2
Location: Dallas
Yaa I can use tht Restriction(less than Or Greater than for the Date Range).

BUT iS IT POSSIBLE TO USE TO_DATE OR DATE(in DB2) sql functions using CRITERIA API's ?

Note: I used this function TO_DATE with normal HQL query and it works..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 5:28 pm 
Newbie

Joined: Sat Jun 14, 2008 6:42 pm
Posts: 4
I'm also looking how to use TO_DATE function in Criteria.

Here is the requirement -

I'm adding code like this
Code:
criteria.add(Restrictions.eq("insertdate", date));


It is generating the SQL like below

Quote:
where insertdate = '22 OCT 2008'


Which in turn returns no records.

If i'm able to use TO_DATE function there then it could've returned records

Anybody has an idea?Pls respond.

Thanks,
Raj


Top
 Profile  
 
 Post subject: Here is the solution
PostPosted: Wed Feb 18, 2009 6:51 pm 
Newbie

Joined: Sat Jun 14, 2008 6:42 pm
Posts: 4
We can add literal sql queries to Critiria like below..

Code:
criteria.add(Restrictions.sqlRestriction("to_date({alias}.insertdate,'dd/mm/rrrr') = "+dateToStringFormat(date)));


and make sure that the dateToStringFormat method returns String as
to_date('22/10/2008', 'dd/mm/rrrr').


The result would be
and to_date(this_.insertdate,'dd/mm/rrrr') = to_date('22/10/2008','dd/mm/rrrr')

The {alias} is required only if multiple tables are used in the query and the same column name exits in those tables.

Hope this helps atlease those who will search for this functionality from now on..


Raj


Top
 Profile  
 
 Post subject: Re: How to use TO_DATE SQl function with Criteria API
PostPosted: Tue Nov 30, 2010 9:19 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
@rk1891 Your solution would work on one database system only.

Anyway to have it database system agnostic ?


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