排序依据
我目前有以下标准: -
Criteria addr = criteria.createCriteria("location.address");
addr.add((Restrictions.and(Restrictions.between("latitude", latmin,
latmax), Restrictions.between("longitude", truelongmin, truelongmax))));
String sql = "SQRT( POW( 69.1 * ( {alias}.latitude - " + point[1]
+" ) , 2 ) + POW( 69.1 * ( "+point[0] +" - {alias}.longitude ) * COS( {alias}.latitude /"
+" 57.3 ) , 2 ) ) < "+distance;
addr.add(Restrictions.sqlRestriction(sql));
我想做的最好的事情是能够按距离排序。 相当于mySQL的是: -
SELECT * , (
SQRT( POW( 69.1 * ( latitude - 51.3814282 ) , 2 ) + POW( 69.1 * ( - 2.3574537 - longitude ) * COS( latitude / 57.3 ) , 2 ) )
) AS distance
FROM `address`
WHERE (
SQRT( POW( 69.1 * ( latitude - 51.3814282 ) , 2 ) + POW( 69.1 * ( - 2.3574537 - longitude ) * COS( latitude / 57.3 ) , 2 ) )
) < 10.0
ORDER BY distance DESC
LIMIT 0 , 30
在Hibernate中做这件事的最好方法是什么? 我试过createCriteria / createAlias(“distance”,“function”)
理想情况下,我想这样做后张对Latmin和Latmax的限制,因为这会降低结果集,从而计算的数量(也是SQL做两次相同的计算:S),但任何建议将不胜感激。
干杯,罗布
Hibernate支持原生查询只是为了这样的情况:)
编辑**
我没有测试过,但类似下面的内容应该可以工作:
StringBuilder sql = new StringBuilder();
sql.append("SELECT *");
sql.append(", ( SQRT( POW( 69.1 * ( latitude - 51.3814282 ) , 2 ) + POW( 69.1 * ( - 2.3574537 - longitude ) * COS( latitude / 57.3 ) , 2 ) ) ) AS distance");
sql.append(" FROM `address`");
sql.append(" WHERE ( SQRT( POW( 69.1 * ( latitude - 51.3814282 ) , 2 ) + POW( 69.1 * ( - 2.3574537 - longitude ) * COS( latitude / 57.3 ) , 2 ) ) ) < 10.0");
sql.append(" ORDER BY distance DESC");
sql.append(" LIMIT 0 , 30");
SQLQuery query = session.createSQLQuery(sql.toString());
// call query.addScalar(..) for other fields you select
query.addScalar("distance", Hibernate.BIG_DECIMAL)
//Where AddressBean is the object that maps to a row of this resultset
query.setResultTransformer(Transformers.aliasToBean(AddressBean.class));
List<AddressBean> list = new ArrayList<AddressBean>(100);
for (Object object : query.list()) {
list.add((AddressBean)object);
}
链接地址: http://www.djcxy.com/p/36977.html
上一篇: Order By