users@glassfish.java.net

Re: possible to use a JPA Query to get filtered Entity AND Relationship?

From: Markus Fuchs <Markus.Fuchs_at_Sun.COM>
Date: Wed, 21 Feb 2007 12:10:35 -0800

Hi Tom,

JPA queries are not intended to return incompletely initialized fields.
As the Department in question really contains Employees besides those
having more than 15 years of service, the employeeCollection will always
contain all Employees from that Department.

A query returning a Collection containing the Employees with more that
15 years of service is:

select e from Employee e where e.department.deptno = 100 and
e.yearsOfService >= 15

Tom Mutdosch wrote:
> Hi there,
> I have a question regarding JPA Query
>
> I have a DEPARTMENT entity with a one-to-many relationship to an
> EMPLOYEE entity. Say I want to get "departments with a department
> number of 100 and containing EMPLOYEES with more than 15 years of
> service".
>
> Is it possible to get back a single result containing the DEPARTMENT
> object; and its relationship collection list consisting of only the
> EMPLOYEES matching the criteria?
>
No. Please see above.

> I've tried creating various queries using joins, but they always
> return me back the DEPARTMENT I want, but the its employee list
> contains ALL of the employees belonging to that department, not just
> the ones matching the "15 years of service" filter.
>
> For example:
> select d from Department d LEFT JOIN d.employeeCollection e WHERE
> d.deptno = 100 AND e.yearsOfService >= 15
>
> This returns one result - a Department with ALL of the employees that
> belong to that department (not just the ones matching e.yearsOfService
> >= 15)
>
> I also tried this:
> select d, e from Department d LEFT JOIN d.employeeCollection e WHERE
> d.deptno = 100 AND e.yearsOfService >= 15
>
> And that returns 3 results, each an array containing a Department and
> Employee. This is giving me the right data (there are 3 employees
> that match the criteria), but I really just want to get back a single
> department result containing the filtered employees. Is this possible?
>
No. How should the expected result look like in this case? An array
containing one Department and three Employees?

HTH,

-- markus.
> I'd appreciate any help; I realize I was blathering a way a bit at the
> end there :)
>
> Thanks
> Tom
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>