문제

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top