是否可以通过使用Hibernate标准获取Order的连接表的SQL别名?

时间:2014-06-12 07:16:36

标签: java mysql hibernate criteria

我有这种类型的查询

Critetria criteria = session.createCriteria(Perosn.class,"person");
criteria.createAlias("person.cityInformation","city",JoinType.LEFT_OUTER_JOIN);
criteria.createAlias("city.streetInformation","street",JoinType.LEFT_OUTER_JOIN);
criteria.add(new NativeSQLOrder("field({alias}.id,3,2,1)",true)));

NativeSQLOrder.java

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Order;

public class NativeSQLOrder extends Order
{
    private static final long   serialVersionUID    = 1L;
    private final static String PROPERTY_NAME       = "uselessAnyways";
    private boolean             ascending;
    private String              sql;

    protected NativeSQLOrder(String sql, boolean ascending)
    {
        super(PROPERTY_NAME, ascending);
        this.sql = sql;
        this.ascending = ascending;
    }

    @Override
    public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException
    {
        StringBuilder fragment = new StringBuilder();
        fragment.append("(");
        fragment.append(sql);
        fragment.append(")");
        fragment.append(ascending ? " asc" : " desc");
        return StringHelper.replace(sql, "{alias}", criteriaQuery.getSQLAlias(criteria));
    }
}

我想通过街道字段的ID订购但是使用这个我得到第一个表的别名。是否有任何方式我将得到别名的街道

1 个答案:

答案 0 :(得分:0)