Kushagr Tyagi
Kushagr Tyagi

Reputation: 101

Getting java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token after running query

I have this old code which uses hibernate to talk to a mySQL DB. I am trying to add a new entry to a table and in doing so this particular section of code is hit.

Query query = em.createQuery(" SELECT MAX(CAST(value,integer)) FROM StaticData where type=:type");
query.setParameter("type", type);
x = query.getResultList();

But it is throwing an exception as follows

[http-nio-8080-exec-153] StaticDataManager  - java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 1, column 23 [ SELECT MAX(CAST(value,integer)) FROM StaticData where type=:type]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:725)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:113)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:362)
    at com.sun.proxy.$Proxy1064.createQuery(Unknown Source)

The Columns type and value are of type String.

What does this error mean? I don't know a lot about hibernate, sorry for asking anything stupid. Thanks in advance for any help.

Upvotes: 0

Views: 1721

Answers (1)

Kirit
Kirit

Reputation: 295

From your exception message

QuerySyntaxException: unexpected token: , near line 1, column 23

it says that it received the comma token, which is not what it expects.

A quick lookup on the HQL cast function suggests that the syntax of cast is CAST( as

Perhaps try

Query query = em.createQuery(" SELECT MAX(CAST(value as integer)) FROM StaticData where type=:type");

Upvotes: 1

Related Questions