简体   繁体   English

Grails GORM withCriteria Alias

[英]Grails GORM withCriteria Alias

I have set up a gorm criteria query using grails. 我已经使用grails设置了一个gorm标准查询。 There are some sqlProjections included. 有一些sqlProjections包括在内。 Now I need to order by this sqlProjections. 现在我需要通过这个sqlProjections来订购。

    def serviceList = Service.withCriteria {

        projections {
            property('id', 'id')
            property('lastServiceMileage', 'lastServiceMileage')
            property('nextServiceMileage', 'nextServiceMileage')
            sqlProjection('(SELECT MAX(e.mileage) FROM tbl_maxmileage AS e WHERE e.v_id = {alias}.v_id) AS mileage', ['mileage'], [INTEGER])
            sqlProjection('(ABS((SELECT mileage) - {alias}.last_service_mileage) / ({alias}.next_service_mileage - {alias}.last_service_mileage)) * 100  AS nextServiceInPercent', ['nextServiceInPercent'], [INTEGER])
            sqlProjection('{alias}.next_service_mileage - (SELECT mileage) AS nextServiceIn', ['nextServiceIn'], [INTEGER])
        }

        if (params.sort && params.order) {
            order(params.sort, params.order)
        }

        firstResult(params.int('offset'))
        maxResults(params.int('max'))

        setResultTransformer(Transformers.aliasToBean(Service.class))

    }

Hibernate fails and prints that eg. Hibernate失败并打印出来,例如。 'nextServiceIn' is not found if params.sort = nextServiceIn 如果params.sort = nextServiceIn,则找不到“nextServiceIn”

I think it is because of the reason I have not written an alias infront of 'nextServiceIn'. 我认为这是因为我没有在'nextServiceIn'前面​​写一个别名。

Now I need to know how to define an criteria root alias with 'withCriteria' 现在我需要知道如何使用'withCriteria'定义标准根别名

Using the session Object it could be done with 使用会话对象可以完成

    hibernateSession.createCriteria(Service.class, "s");

I had a look to the raw query generated by hibernate. 我查看了hibernate生成的原始查询。 I noticed that 'nextServiceIn' is printed there without any alias as prefix. 我注意到'nextServiceIn'打印在那里没有任何别名作为前缀。

eg 'order by nextServiceIn asc' ... 例如'by nextServiceIn asc'...

Now the solution is to extend org.hibernate.criterion.Order and implement a basic ordering without any lookup. 现在解决方案是扩展org.hibernate.criterion.Order并实现基本排序而不进行任何查找。

import org.hibernate.criterion.Order
import org.hibernate.criterion.CriteriaQuery
import org.hibernate.Criteria
import org.hibernate.HibernateException

/**
 * Created with IntelliJ IDEA.
 * User: pheinrich
 * Date: 18.01.16
 * Time: 16:33
 * To change this template use File | Settings | File Templates.
 */
public class OrderBySql extends Order {

    private String sqlOrderString;

    /**
     * Constructor for Order.
     * @param sqlOrderString an SQL order that will be appended to the resulting SQL query
     */
    protected OrderBySql(String sqlOrderString) {
        super(sqlOrderString, true);
        this.sqlOrderString = sqlOrderString;
    }

    public String toString() {
        return sqlOrderString;
    }

    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
        return sqlOrderString;
    }

    /**
     * Custom order
     *
     * @param sqlProperty an SQL property that will be appended to the resulting SQL query
     * @param sqlOrder an SQL order that will be appended to the resulting SQL query
     * @return Order
     */
    public static Order sqlOrder(String sqlProperty, String sqlOrder) {
        return new OrderBySql(sqlProperty + " " + sqlOrder);
    }

}

And use it inside your GORM criteria 并在GORM标准中使用它

def serviceList = Service.withCriteria {

    projections {
        property('id', 'id')
        property('lastServiceMileage', 'lastServiceMileage')
        property('nextServiceMileage', 'nextServiceMileage')
        sqlProjection('(SELECT MAX(e.mileage) FROM tbl_maxmileage AS e WHERE e.v_id = {alias}.v_id) AS mileage', ['mileage'], [INTEGER])
        sqlProjection('(ABS((SELECT mileage) - {alias}.last_service_mileage) / ({alias}.next_service_mileage - {alias}.last_service_mileage)) * 100  AS nextServiceInPercent', ['nextServiceInPercent'], [INTEGER])
        sqlProjection('{alias}.next_service_mileage - (SELECT mileage) AS nextServiceIn', ['nextServiceIn'], [INTEGER])
    }

    if (params.sort && params.order) {

        // special handling for sqlProjection alias
        if (params.sort == "nextServiceIn") {
            order(OrderBySql.sqlOrder(params.sort, params.order))
        } else {
            order(params.sort, params.order)
        }

    }

    firstResult(params.int('offset'))
    maxResults(params.int('max'))

    setResultTransformer(Transformers.aliasToBean(Service.class))

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM