Jacob Jacob - 8 months ago 25
Java Question

How to write JPA query where parameter is a set?

Assuming the following class, how do you find a

with a particular email address?

public class Person implements Comparable<Person> {

@GeneratedValue(strategy = GenerationType.AUTO)
private long id = 0;

@OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, fetch=FetchType.LAZY)
private Set<String> email = new HashSet<String>();

Is it as simple as doing just this, or is there a proper way?

select p from Person p where p.email=:email


It's not that easy. JPQL provides the IN operator for this:

select p from Person p, IN(p.email) m where m = :email

The 'old' way (read SQL-like) would be:

select p from Person p join p.email m where m = :email