Last active
August 29, 2015 14:20
-
-
Save veryyoung/65fd663b0cd40f435871 to your computer and use it in GitHub Desktop.
扩展hibernate自定义排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.hibernate.Criteria; | |
| import org.hibernate.HibernateException; | |
| import org.hibernate.criterion.CriteriaQuery; | |
| import org.hibernate.criterion.Order; | |
| /** | |
| * Created by veryyoung on 2015/5/7. | |
| * 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 OrderBySqlFormulaUtils extends Order { | |
| private String sqlFormula; | |
| /** | |
| * Constructor for Order. | |
| * | |
| * @param sqlFormula an SQL formula that will be appended to the resulting SQL query | |
| */ | |
| protected OrderBySqlFormulaUtils(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 OrderBySqlFormulaUtils(sqlFormula); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment