23
return sessionFactory.getCurrentSession().
            createQuery("FROM Weather WHERE city_id = :id AND date " +
                    "BETWEEN now()::date AND now()::date + (:days - 1)").
                    setInteger("id", city_id).setString("days", days).list();

getting error:

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: :

How can I use this syntax in HQL?

Basically the problem is that I want to use colon(:) in my query, but when hibernate sees colon, it thinks that it is a paramter(:parameterName is syntax for parameters in HQL), as you can see from my 2 uses(:id and :days).

But when I am using now()::date statement, it is specific postgreSQL syntax, hibernate ruins everything.

Rupesh Yadav
  • 14,336
  • 5
  • 53
  • 68
Jaanus
  • 15,013
  • 43
  • 137
  • 193
  • what are you trying to achieve ? cast ? – stratwine Sep 19 '11 at 19:02
  • No .. just wanna use my SQL query, but Hibernate marks it as invalid. – Jaanus Sep 19 '11 at 19:18
  • dup : http://stackoverflow.com/q/2794972/106261 – NimChimpsky Sep 19 '11 at 19:53
  • Sorry, but you can't use SQL in place of HQL. You have supplied an SQL query. See http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html and try and write your query using valid HQL. Or use a native query. – James DW Sep 19 '11 at 20:30
  • 2
    @James : Isn't `createSQLQuery` used for native queries? I tried that but now getting a little different error : `Not all named parameters have been set: [:date]` – Jaanus Sep 19 '11 at 20:55

6 Answers6

41

I just had this problem, had to use casts, so I tried some stuff to make it work. Turns out you escape : in hibernate with \

However, in java, to print \ to begin with, you have to escape it with \.
So, if you want to put a : in your SQL hibernate query, you have to write it like: \\:

And if you wanted to cast in PostgreSQL, such as in my case, you would have to, for example: field\\:\\:int if you wanted to cast some field as an integer.

nhahtdh
  • 52,949
  • 15
  • 113
  • 149
SwampDev
  • 607
  • 5
  • 9
4

Since you're on Postgres, I would change the date() completely:

return sessionFactory.getCurrentSession().
        createQuery("FROM Weather WHERE city_id = :id AND date " +
                "BETWEEN current_date AND (current_date + (integer :days - 1))").
                setInteger("id", city_id).setString("days", days).list();

See http://www.postgresql.org/docs/8.2/static/functions-datetime.html

atrain
  • 8,835
  • 1
  • 33
  • 39
  • 4
    Maybe try using CAST instead of ::. See http://stackoverflow.com/questions/4791325/how-do-i-write-hql-query-with-cast – atrain Sep 19 '11 at 20:11
3

Take a look at http://www.postgresql.org/docs/8.1/static/sql-createcast.html

Try using cast. To me it worked like a charm.

0
return sessionFactory.getCurrentSession().
        createQuery("FROM Weather WHERE city_id = :id AND date " +
                "BETWEEN cast(now() as date) AND cast(now() as date) + (:days - 1)").
                setInteger("id", city_id).setString("days", days).list();
-1

Named parameters take a colon ':' like this Is that what you were looking for ?

stratwine
  • 3,525
  • 2
  • 24
  • 32
  • 1
    Nope...this is actually the problem. I am just trying to use colons in my query, but hibernate thinks that they are parameters. – Jaanus Sep 19 '11 at 19:20
-1

You escape : with ::. I think.

Or try a nativequery

NimChimpsky
  • 43,542
  • 55
  • 186
  • 295