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.