Passing empty list as parameter to JPA query throws error

后端 未结 7 1783
无人共我
无人共我 2020-12-05 22:52

If I pass an empty list into a JPA query, I get an error. For example:

List municipalities = myDao.findAll();  // returns empty list
em.c         


        
7条回答
  •  天命终不由人
    2020-12-05 23:44

    According to the section 4.6.8 In Expressions from the JPA 1.0 specification:

    There must be at least one element in the comma separated list that defines the set of values for the IN expression.

    In other words, regardless of Hibernate's ability to parse the query and to pass an IN(), regardless of the support of this syntax by particular databases (PosgreSQL doesn't according to the Jira issue), you should use a dynamic query here if you want your code to be portable (and I usually prefer to use the Criteria API for dynamic queries).

提交回复
热议问题