Dynamic query using hibernate criteria api -
i want build query dynamically using hibernate criteria api.i facing problem "order by".suppose have statement
criteria crit = session.createcriteria(product.class); crit.addorder(order.asc("price"));
now want control "asc" or "desc" based on user input.is possible?
the criteria object api provides order class main api manage sorting.the order class has 2 methods set sorting order:
asc(string attribute) : sorts query attribute in ascending order.
desc(string attribute) : sorts query attribute in descending order.
now if want control "asc" or "desc" based on user input can following
let assume user input can "asc" or "desc".
criteria crit = session.createcriteria(product.class); if (userinput.equals("asc")) { crit.addorder(order.asc("price")); } else if (userinput.equals("desc")) { crit.addorder(order.desc("price")); }
Comments
Post a Comment