-->
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.  [ 1 post ] 
Author Message
 Post subject: Using ABS() function with Criteria
PostPosted: Tue Aug 29, 2006 3:59 am 
Newbie

Joined: Tue Aug 29, 2006 3:07 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.2

I'm writing a Query containing ABS() function using DetachedCriteria.
My SQL query should be "select * from MyTable mt where ABS(mt.T_AMOUNT) >= 100 and ABS(mt.T_AMOUNT) <= 5000"
Now I would try to write this query using a DetachedCriteria with Criterion.
In file MyTable.hbm.xml the entity-name is MyTable and the Database column "T_AMOUNT" is mapped with property_name "amount".

Here follows code that build Criteria:

DetachedCriteria criteria = DetachedCriteria.forEntityName("MyTable");
criteria.add(Restrictions.ge("amount", 100));
criteria.add(Restrictions.le("amount", 5000));


The problem is How to apply the ABS function to the property "amount" using Hibernate Criteria API ?

I tried this:
criteria.add(Restrictions.sqlRestriction("ABS(T_AMOUNT) >= 100");
criteria.add(Restrictions.sqlRestriction("ABS(T_AMOUNT) <= 5000");


At the end I call
getHibernateTemplate().findByCriteria(criteria);
and I have expected results without problems

But I wouldn't write into Java code the name of Database column.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.