HOW TO ORDER BY A CUSTOM SQL FORMULA/EXPRESSION WHEN USING HIBERNATE CRITERIA API
来源:互联网 发布:php信息发布系统源码 编辑:程序博客网 时间:2024/06/11 06:08
In our current project we are using Spring + Hibernate Annotations.
Today I needed to use something like:
SELECT DISTINCT t.id FROM MyClass t WHERE ..... ORDER BY (a + b) DESC
where a and b are properties of MyClass (columns in the “my_class” table).
The “where …” expression must be generated dynamically based on the user input, so we are using Criteria API to generate the query as such:
Criteria criteria = getSession().createCriteria(MyClass.class);criteria.setProjection(Projections.distinct(Projections.id())); // Some custom dynamic conditionscriteria.add(Restrictions.gt("createdDate", afterDate));criteria.add(Restrictions.in("state", approvedStates));criteria.add(Restrictions.isNull("deletedDate"));if (includedCategories != null || excludedCategories != null) { Criteria categoryCriteria = criteria.createCriteria("category"); if (includedCategories != null) { for (String categoryPrefix : includedCategories) { categoryCriteria.add(Restrictions.like("path", categoryPrefix + "%")); } } if (excludedCategories != null) { for (String categoryPrefix : excludedCategories) { categoryCriteria.add(Restrictions.not(Restrictions.like("path", categoryPrefix + "%"))); } }} criteria.add(Restrictions.sqlRestriction("(a + b) > 1")); // Custom ordering by some SQL formula/expressioncriteria.addOrder(Order.desc("a + b")); return criteria.list();
Now, the problem is that the class Order does not support custom SQL formula/expression…
So my solution was to derive my own class as such:
package ro.tremend.util.hibernate; import org.hibernate.criterion.Order;import org.hibernate.criterion.CriteriaQuery;import org.hibernate.Criteria;import org.hibernate.HibernateException; /** * Extends {@link org.hibernate.criterion.Order} to allow ordering by an SQL formula passed by the user. * Is simply appends the <code>sqlFormula</code> passed by the user to the resulting SQL query, without any verification. * @author Sorin Postelnicu * @since Jun 10, 2008 */public class OrderBySqlFormula extends Order { private String sqlFormula; /** * Constructor for Order. * @param sqlFormula an SQL formula that will be appended to the resulting SQL query */ 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; } /** * Custom order * * @param sqlFormula an SQL formula that will be appended to the resulting SQL query * @return Order */ public static Order sqlFormula(String sqlFormula) { return new OrderBySqlFormula(sqlFormula); }}
criteria.addOrder(OrderBySqlFormula.sqlFormula("(a + b) desc"));
0 0
- HOW TO ORDER BY A CUSTOM SQL FORMULA/EXPRESSION WHEN USING HIBERNATE CRITERIA API
- LINQ to SQL系列Part 9-Using a Custom LINQ Expression with the control
- How To Transact Move Order Using INV_PICK_WAVE_PICK_CONFIRM_PUB.Pick_Confirm API
- SQL: How to get a series of dates in sql using connect by
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- How to simulate a Form POST request by using WinInet
- How to simulate a Form POST request by using WinInet
- How to simulate a Form POST request by using WinInet
- SD-How to get delivery plant when you use va01 to create a new order
- How To - Create a custom field using attributes of other UI objects
- 改进:How To - Create a custom field using attributes of other UI objects
- How to make a custom keyboard in iOS 8 using Swift
- How to Simulate the Price Order or Price Line Function using API QP_PREQ_PUB.PRICE_REQUEST Includes
- How to Hack a WordPress Site using SQL Injection
- How to synchronize access to a shared resource in a multithreading environment by using Visual C#
- how to display a loading gif when using jquery ui dialog iframe
- how to select data in multiple datatables after using join,group by and order by in Linq
- FR按钮调用数据库(MMSQL)存储过程,参数储存有返回值
- CSU 1866 中南大学网络赛A题 Apache and new sports competition
- POJ 3181 Dollar Dayz(完全背包+大数)
- Hibernate缓存
- 关于递归的一些思考
- HOW TO ORDER BY A CUSTOM SQL FORMULA/EXPRESSION WHEN USING HIBERNATE CRITERIA API
- 安卓缓存文件
- 输入一个数字,转换成字符串
- Course Selection System 01背包
- Loadrunner中脚本的迭代次数和场景运行时间的关系
- Apache Spark vs Apache Flink
- CSU 1868 中南大学网络赛B题 潜在好友
- Vector3.Lerp
- 欢迎使用CSDN-markdown编辑器