I want to select something like this MySQL query, how do it in hibernate criteria?
SELECT id, IF(type = 'P', amount, amount * -1) as amount FROM report
I want to select something like this MySQL query, how do it in hibernate criteria?
SELECT id, IF(type = 'P', amount, amount * -1) as amount FROM report
I'm not sure how to do it using a criteria, but you can acheive the same by executing a native sql query in hibernate. Lets assume your model class is "Report" and it has a property called "amount" with its getter and setter:
String query = "SELECT id, IF(type = 'P', amount, amount * -1) as amount FROM report";
Session session = sessionFactory.openSession();
SQLQuery sqlQuery = session.createSQLQuery(query);
sqlQuery.setResultTransformer(Transformers.aliasToBean(Report.class));
List<Report> resultList = sqlQuery.list();
session.close();