-->
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: calculated field in criteria?
PostPosted: Wed Jan 18, 2006 8:45 am 
Newbie

Joined: Tue Jun 14, 2005 12:44 pm
Posts: 19
Hi there

I would like to find a calculated field inside my DetachedCriteria query and return it as part of the result.

For example: I have an end date and a start date and I want to calculate the duration between the two (the exact field is not in the database object)

I have not been able to do this - any ideas if it's possible? Projection somehow ?

And if this is possible - is it possible to do an orderBy on the calculated field?


Thanks++


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 1:29 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
"I have an end date and a start date and I want to calculate the duration between the two"

What are u going to do with this?

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: goal
PostPosted: Thu Jan 19, 2006 4:18 am 
Newbie

Joined: Tue Jun 14, 2005 12:44 pm
Posts: 19
I'd like to get the result of the duration in the result set, as well as run an order by on it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 6:29 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
Try sql projection. Something like

criteria.setProjection(Projections.sqlProjection("(enddate - startdate) as duration", new String[]{"duration"}, new org.hibernate.type.DateType[]{new org.hibernate.type.DateType()}),"duration");

and then

criteria.addOrder((Order.asc("duration")).ignoreCase());

Please check if DateType is the correct type of the value returned by sql fragment.


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.