Rakhi Sharma
Rakhi Sharma

Reputation: 119

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: u

I have created a method to get user count. This is my JPARepository. But I am getting QuerySyntaxException. I do not know how to solve this.

@Query("SELECT COUNT(*) from User u where u.usertype is NULL u.country=?1 and u.state=?2 and u.city=?3 and u.datasource=?4")
int getusercount();

But When I run the code, I got:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: u near line 1, column 78 [SELECT COUNT(*) from User u where u.usertype is NULL u.country=?1 and u.state=?2 and u.city=?3 and u.datasource=?4]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
atorg.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
atorg.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_191]
atorg.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at com.sun.proxy.$Proxy125.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87) ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]

Upvotes: 0

Views: 3349

Answers (1)

Beshambher Chaukhwan
Beshambher Chaukhwan

Reputation: 1448

The problem is that you forgot to add an and operator in your query after the first condition. The query should be like

 @Query("SELECT COUNT(*) from User u where u.usertype is NULL and u.country=?1 and u.state=?2 and u.city=?3 and u.datasource=?4") 

The exception org.hibernate.hql.internal.ast.QuerySyntaxException is self explanatory that there is a syntax error in the query. It says unexpected token: u near line 1, column 78 means that after the condition u.usertype in NULL it expects some operator or some other sql keyword such as order by etc but instead it has u.country which is unexpected.

Upvotes: 2

Related Questions