Passing empty list as parameter to JPA query throws error

后端 未结 7 1773
无人共我
无人共我 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:39

    Assuming the SQL query to be like

    (COALESCE(:placeHolderName,NULL) IS NULL OR Column_Name in (:placeHolderName))
    

    Now, If the List is of type String then you can pass as

    query.setParameterList("placeHolderName", 
    !CollectionUtils.isEmpty(list)? list : new ArrayList(Arrays.asList("")).
    

    And If the List is containing the Integer values then the syntax is like below:

    If(!CollectionUtils.isEmpty(list)){
    query.setParameterList("placeHolderName",list)
    }else{
    query.setParameter("placeHolderName",null, Hibernate.INTEGER)
    }
    
    

提交回复
热议问题