Question

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

No correct solution

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top