-->
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: Hibernate Criteria ( Difference between two column)
PostPosted: Tue Dec 15, 2009 5:25 pm 
Newbie

Joined: Tue Dec 15, 2009 5:18 pm
Posts: 1
Hi,
Iam trying to use Hibernate Criteria API to create Dynamic Queries.
I wanted to find the difference between two dataresultset from a table.

Sum(col1-col2) from table_name
where col3=1;

How to do the difference part using Hibernate Criteria.
Please Help me with this.
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria ( Difference between two column)
PostPosted: Wed Dec 16, 2009 12:55 pm 
Newbie

Joined: Wed Dec 16, 2009 12:24 pm
Posts: 6
Location: Munich
It's possible to do arithmetic operations with Projections.sqlProjection().

Example:

Code:
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.sqlProjection("this_.id * 1000 as multiId", new String[] {"multiId"}, new Type[]{Hibernate.INTEGER}));
criteria.list();


The problem is that you have to use SQL aliases to access columns. I've described a solution at http://blog.bambo.it/2009/12/generic-subcriterias-for-hibernate.html and http://blog.bambo.it/2009/12/arithmetic-operations-using-generic.html


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.