1

I need to convert below query in hibernate criteria.

Any one have idea how to write this order by clause in hibernate?

select * from StudentMasterTable where SchoolId = 90 and SchoolName IS NOT NULL ORDER BY IIF(StudentType = 'O',StudentName,SchoolName) 

2 Answers 2

1

finally i found solution for this.

i created a custom class to override the criteria formula.

public class customOrderBy extends Order { private String sqlFormula; protected OrderBySqlFormula(String sqlFormula) { super(sqlFormula, true); this.sqlFormula = sqlFormula; } public String toString() { return sqlFormula; } public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { return sqlFormula; } public static Order customOrder(String sqlFormula) { return new OrderBySqlFormula(sqlFormula); } } 

And i called this as below

criteria.addOrder(customOrderBy.customOrder("IIF(StudentType = 'O',StudentName,SchoolName)")); 
Sign up to request clarification or add additional context in comments.

Comments

0

I believe i did something like this to deal with dynamic orderBys:

//make a method that will take in a string which is I guess your studentType column(you probably should have shown your entity class for StudentMasterTable, I assumed studentType was type of String).

String orderByClause(String s){ String result = ""; if(s.equals('O')) result = "s.studentType"; return result; } 

//then do the below

"select s from StudentMasterTable s where s.schoolId = '90' and s.schoolName is not null order by " + orderByClause(s.studentType) + ",s.studentName,s.schoolName"; 

1 Comment

i dont have StudentType in request i need to do order by by checking the data which is in table or in bean looking for criteria based solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.