After struggling most of the day I've come up with this. Of course now it seems that it probably should have been clear previously.
String hql = " select acos( " +
" sin(:lat) * sin(biz.address.latitude/57.29) + " +
" cos(:lat) * cos(biz.address.latitude/57.29) * " +
" cos(biz.address.longitude/57.29 - :lon) " +
" ) * 3956 as distance, biz " +
"from Business biz " +
"where biz.name like :criteria " +
// "having distance < :radius ";
" order by distance asc";
Query query = session.createQuery(hql)
.setInteger("radius", 5)
.setString("criteria", "%auto%")
.setDouble("lat", lat)
.setDouble("lon", lon).
setMaxResults(10);
List list = query.list();
Iterator results = list.iterator();
for (int i = 0; i < 5; i++)
{
Object[] arr = (Object[])results.next();
Double dbdist = (Double)arr[0];
Business biz = (Business)arr[1];
}
The only problem I have now is the "having distance < :radius" ... hibernate is throwing this exception. I got a very similar query to run fine in mysql. Trace below:
Nov 15, 2005 10:03:28 PM org.hibernate.hql.ast.ErrorCounter reportError
SEVERE: line 1:241: unexpected token: having
E
Time: 2.648
There was 1 error:
1) testHqlSearch(HibernateUnitTest)org.hibernate.hql.ast.QuerySyntaxException: unexpected token: having near line 1, column 241 [ select acos( sin(:lat) * sin(biz.address.latitude/57.29) + cos(:lat) * cos(biz.address.latitude/57.29) * cos(biz.address.longitude/57.29 - :lon) ) * 3956 as distance, biz from com.chaos.value.Business biz where biz.name like :criteria having distance < :radius order by distance asc]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:240)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:151)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:101)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:468)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1025)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:975)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at com.chaos.unittest.HibernateUnitTest.testHqlSearch(HibernateUnitTest.java:464)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Caused by: line 1:241: unexpected token: having
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:743)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:234)
... 22 more
|