12

this is the DAO method i have to retrieve the list of students from DB and on RunTime it says -

org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"

which is driving me crazy. Can anyone please tell me what i might have missed?

public List<Student> getStudentsByIds(List<Integer> studentIds) {
    Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
    SQLQuery query = session.createSQLQuery("SELECT * FROM students s WHERE s.id IN (:studentIds)");
    query.setParameterList("studentIds", studentIds);
    return query.list();
}
5
  • If you print your "studentIds" list what do you get? are you sure it isn't wrapping an additional ")" inside the SQL? Commented Aug 10, 2016 at 15:56
  • I get list of Integers. Yeah, I'm sure its not wrapping Commented Aug 10, 2016 at 16:06
  • Shouldn't you use something like id = ANY(:studentIds) since you are providing an array Commented Aug 10, 2016 at 16:13
  • Have you tried using an array instead of a list? Commented Aug 10, 2016 at 16:28
  • Changing SQLQuery query = .... to Query query = .... made the error go away. Anybody know why? Commented Aug 10, 2016 at 22:44

2 Answers 2

13

Probably studentIds is an empty list and postgres does not accept empty IN clause in generated code

select 
...
where
    students.id in (

    )
Sign up to request clarification or add additional context in comments.

Comments

1

Had this issue recently. So as I got from docs psql can't handle empty lists in IN/NOT IN (:someEmptyList) statements cause criteriaBuilder adds empty curly braces into final sql query: (in ())

Imho the easiest solution will be just to check for empty list and return empty result list back.

Or use more advanced tools if u have complex query and u really need it => https://marcelothebuilder.github.io/java/jpa/2017/09/11/jpa-post.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.