0

I try use this HQL query:

   Result.find("SELECT c, ( 3959 * acos( cos( radians(?) ) * "+
    "cos( radians( c.latitude ) ) *"+
    "cos( radians( c.longitude ) - radians(?) ) +"+
    "sin( radians(?) ) * sin( radians( c.latitude ) ) ) ) " +
    "AS distance FROM City c HAVING distance < ? ORDER BY distance ASC",
    latitude, longitude, latitude, radius).fetch();

But in result:

IllegalArgumentException occured : org.hibernate.hql.ast.QuerySyntaxException: unexpected token: HAVING near line 1, column 204 [SELECT c, ( 3959 * acos( cos( radians(?) ) * cos( radians( c.latitude ) ) *cos( radians( c.longitude ) - radians(?) ) +sin( radians(?) ) * sin( radians( c.latitude ) ) ) ) AS distance FROM models.City c HAVING distance < ? ORDER BY distance ASC]

5 Answers 5

1

try changing HAVING to WHERE, in your query.

Sign up to request clarification or add additional context in comments.

5 Comments

Now Unknown column 'distance' in 'where clause'
distance is alias for the column, you have to put the whole calculation as in select, in where clause as well. instead of column alias
But then I can not sort the list of cities.
Hm... Unknown column 'col_0_0_' in 'order clause'
This query work: SELECT c, ( … ) AS distance FROM City c ORDER BY distance ASC. But without WHERE
1

The keyword HAVING is only allowed if you use GROUP BY (think of having like WHERE for GROUPS). See the reference manual for SELECT syntax.

3 Comments

Yes, you're right. Now I've corrected the 'having' to 'where'. And got an error: Unknown column 'distance' in 'where clause'
@AntonTsivarev You need to place the expression in a subquery to refer to distance.
@stacker, you cannot use a subquery expression in the from : "Note that HQL subqueries can occur only in the select or where clauses." docs.jboss.org/hibernate/core/4.3/manual/en-US/html_single/…
1

The only way is to use a native query because :

Java code :

@Query(value = "SELECT s.*,\n" +
            "  (\n" +
            "    3959 * acos(\n" +
            "        cos(radians(s.lat))\n" +
            "        * cos(radians(:lat))\n" +
            "        * cos(radians(:lon) - radians(s.lon))\n" +
            "        + sin(radians(s.lat))\n" +
            "          * sin(radians(:lat))\n" +
            "    )\n" +
            "  ) AS distance\n" +
            "FROM Stop s\n" +
            "HAVING distance < 30\n" +
            "ORDER BY distance asc", nativeQuery = true)
    List<Stop> findClosestStops(@Param("lat") Double lat, @Param("lon") Double lon);

An alternative is to return a list of object containg the city + distance, but no where is possible, but you can limit the resultset

1 Comment

Worked for me. I was missing nativeQuery=true param in my code. Just for those who are trying to execute the query in JPA then put, nativeQuery = true and try.
0

Hm, what should be "?" parameter?

If true you should use it like

HAVING distance < :distance

and use

query.setParameter("distance", 50);

Comments

0

try

 select dis.c,dis.distance(
    SELECT c,
     ( 3959 * acos( cos( radians(?) ) *  cos( radians( c.latitude ) ) * 
cos( radians( c.longitude ) - radians(?) ) + sin( radians(?) ) *
 sin( radians( c.latitude ) ) ) )  AS distance FROM City c  ) dis 
where dis.distance<? order by dis.distance ASC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.