I starting with JPA queries. I'm trying to find out how to set a collection as a parameter of 'member of' where clause. Taking the Model described in Ultimate JPA Queries and Tips List – Part 1
private static boolean isThisDogBelongingToAperson(EntityManager em, Dog dog, String name) {
Query query = em.createQuery('select count(p) from Person p where :dog member of p.dogs and p.name = :name');
query.setParameter('dog', dog);
query.setParameter('name', name);
try {
return query.getSingleResult() != null;
} catch (Exception e) {
return false;
}
}
This is taking only one instance of Dog as parameter. What if I have a list of dogs and wnat to create a method named as something like 'isAnyOfTheseDogsBelongingToAperson'. Do I need to call the method above several times for each element for the Dog list, or is there a way to pass the collection to the query? For example:
private static boolean isAnyOfTheseDogsBelongingToAperson(EntityManager em, List<Dog> dogs, String name) {
Query query = em.createQuery('select count(p) from Person p where :dogs member of p.dogs and p.name = :name');
query.setParameter('dogs', dogs);
query.setParameter('name', name);
try {
return query.getSingleResult() != null;
} catch (Exception e) {
return false;
}
}