Q1:
I've made this JPA query that I find really satisfying:
Booking.find("SELECT DISTINCT b FROM Booking b JOIN b.participants AS
t WHERE t = ?", person).fetch();
This query is used for finding all bookings where a given person
participates - and it works just fine.
But now I would like to extend the query so that it will take a list
of employees:
I've tried:
List<Person> persons = Person.findAll();
Booking.find("SELECT DISTINCT b FROM Booking b JOIN b.participants AS
t WHERE t IN ?", persons).fetch();
Which gives me the error:
JPAQueryException occured : Error while executing query SELECT
DISTINCT b FROM Booking b JOIN b.participants AS t WHERE t IN ?:
org.hibernate.HibernateException: Unable to resolve entity name from
Class [java.util.ArrayList] expected instance/subclass of
[models.Person]
I've also tried:
List<Long> ids = new ArrayList<Long>();
ids.add(
employee.id);
Booking.find("SELECT DISTINCT b FROM Booking b JOIN b.participants AS
t WHERE
t.id IN ?", ids)
Which gives me the error:
JPAQueryException occured : Error while executing query SELECT
DISTINCT b FROM Booking b JOIN b.participants AS t WHERE
t.id IN ?:
java.util.ArrayList cannot be cast to java.lang.Long
Heureka! While writing this post, I found a solution. Using the
EntityManager:
List<Person> persons = Person.findAll();
Query query = JPA.em().createQuery("SELECT DISTINCT b FROM Booking b
JOIN b.participants AS t WHERE t IN (?1)");
query.setParameter(1, persons);
So am I using Play JPA wrong? Or is it a missing feature to use WHERE
IN with ? parameteres.
Q2:
Do you have good references where to read about JPA? With lots of
examples :)