Passing empty list as parameter to JPA query throws error
Posted
by Tuukka Mustonen
on Stack Overflow
See other posts from Stack Overflow
or by Tuukka Mustonen
Published on 2010-03-21T22:04:05Z
Indexed on
2010/03/21
22:11 UTC
Read the original article
Hit count: 372
If I pass an empty list into a JPA query, I get an error. For example:
List<Municipality> municipalities = myDao.findAll(); // returns empty list
em.createQuery("SELECT p FROM Profile p JOIN p.municipality m WHERE m IN (:municipalities)")
.setParameter("municipalities", municipalities)
.getResultList();
Because the list is empty, Hibernate generates this in SQL as "IN ()", which gives me error with Hypersonic database.
There is a ticket for this in Hibernate issue tracking but there are not many comments/activity there. I don't know about support in other ORM products or in JPA spec either.
I don't like the idea of having to manually check for null objects and empty lists every time. Is there some commonly known approach/extension to this? How do you handle these situations?
© Stack Overflow or respective owner