Search code examples
javamysqlspringspring-repositories

jdbc4.MySQLSyntaxErrorException: error in your SQL syntax; check the manual MySQL server version for the right syntax to use near ') )' at line 1


I am facing problem when trying to fetch data using crudRepository. i am using native query to fetch count of records from data base but i am getting the following error.

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')  )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_171]

my query in repository is:

@Query(value = "select count(1) from user_records upr  where RECORD_ID in  ( SELECT record_id FROM region_record_relation where region_id in :regionIds) ", nativeQuery = true)
public int findAllRecordsforRegionIds(@Param("regionIds") List<Long> regionIds);

i dont have any idea how to solve this problem. i am using Mysql database.

show-sql setting is turned on. but all i get is:

 2018-08-02 20:06:17.210  WARN 3764 --- [nio-8088-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
 2018-08-02 20:06:17.210 ERROR 3764 --- [nio-8088-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')  )' at line 1
 2018-08-02 20:06:17.231 ERROR 3764 --- [nio-8088-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')  )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_171]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_171]

Solution

  • @Query(value = "select count(1) from user_records upr  where RECORD_ID in  ( SELECT record_id FROM region_record_relation where region_id in :regionIds) 
    

    Should be

    @Query(value = "select count(1) from user_records upr  where RECORD_ID in  ( SELECT record_id FROM region_record_relation where region_id in (:regionIds))) 
    

    There are better ways to write this that would be more efficient. But this should solve the error.