Using Hibernate query : colon gets treated as parameter / escaping colon

19,815

Solution 1

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

Solution 2

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.

Solution 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.

Share:
19,815

Related videos on Youtube

Jaanus
Author by

Jaanus

Doing C#, Java 50-50. SOreadytohelp

Updated on June 04, 2022

Comments

  • Jaanus
    Jaanus almost 2 years
    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.

    • stratwine
      stratwine over 12 years
      what are you trying to achieve ? cast ?
    • Jaanus
      Jaanus over 12 years
      No .. just wanna use my SQL query, but Hibernate marks it as invalid.
    • NimChimpsky
      NimChimpsky over 12 years
    • James DW
      James DW over 12 years
      Sorry, but you can't use SQL in place of HQL. You have supplied an SQL query. See docs.jboss.org/hibernate/core/3.3/reference/en/html/… and try and write your query using valid HQL. Or use a native query.
    • Jaanus
      Jaanus over 12 years
      @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
    Jaanus over 12 years
    Nope...this is actually the problem. I am just trying to use colons in my query, but hibernate thinks that they are parameters.
  • atrain
    atrain over 12 years
    Maybe try using CAST instead of ::. See stackoverflow.com/questions/4791325/…
  • beerbajay
    beerbajay over 9 years
    This did not work for me with hibernate 3.6.10. I ended up using cast(field as integer) instead.
  • Mickaël Gauvin
    Mickaël Gauvin over 8 years
    It worked for me on hibernate 4.3.11.Final sb.append(" NULL\\:\\:DATE AS date, ");
  • zpontikas
    zpontikas over 6 years
    Looks ok to me but please add some comments as to what was wrong :)
  • carlos.romel
    carlos.romel over 6 years
    SQL ANSI don't describe casts with double collons. Tô solve this, use cast function, instead.
  • catch22
    catch22 over 5 years
    Worked for me too with version: '5.0.12.Final'