2

I'm trying to write a HQL/Criteria/Native SQL query that will return all Employees that are assigned to a list of Projects. They must be assigned to all Projects in order to be selected.

An acceptable way of achieving this with native SQL can be found in the answer to this question: T-SQL - How to write query to get records that match ALL records in a many to many join:

SELECT e.id 
FROM employee e 
    INNER JOIN proj_assignment a 
        ON e.id = a.emp_id and a.proj_id IN ([list of project ids])
GROUP BY e.id
HAVING COUNT(*) = [size of list of project ids]

However, I want to select all fields of Employee (e.*). It's not possible to define SQL grouping by all the columns(GROUP BY e.*), DISTINCT should be used instead. Is there a way to use DISTINCT altogether with COUNT(*) to achieve what I want?

I've also tried using HQL to perform this query. The Employee and ProjectAssignment classes don't have an association, so it's not possible to use Criteria to join them. I use a cross join because it's the way to perform a Join without association in HQL. So, my HQL looks like

select emp from Employee emp, ProjectAssignment pa 
where emp.id = pa.empId and pa.paId IN :list 
group by emp having count(*) = :listSize

However, due to a bug in Hibernate, GROUP BY entity does not work. The SQL it outputs is something like group by (emptable.id).

Subquerying the assignment table for each project (dynamically adding and exists (select 1 from proj_assignment pa where pa.emp_id=e.id and pa.proj_id = [anId]) for each project in the list) is not an acceptable option.

Is there a way to write this query properly, preferrably in HQL (in the end I want a List<Employee>), without modifying mappings and without explicitly selecting all columns in the native SQL ?


EDIT: I'm using Oracle 10g and hibernate-annotations-3.3.1.GA

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161

2 Answers2

0

How about:

select * from employee x where x.id in(
SELECT e.id 
FROM employee e 
    INNER JOIN proj_assignment a 
        ON e.id = a.emp_id and a.proj_id IN ([list of project ids])
GROUP BY e.id
HAVING COUNT(*) = [size of list of project ids]
)
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • If I remember correctly, this is legal only in MySQL. I'm using Oracle (edited question to provide this detail). – Xavi López Nov 18 '11 at 08:38
  • As far as I know, this is 'standard' sql, and should work with most databases, including Oracle. Actually, MySQL is the one database where this syntax might need to be altered slightly - I think that MySQL would require the inner select to have an alias. – GreyBeardedGeek Dec 16 '11 at 04:45
0

I've found an alternative way to achieve this in HQL, it's far more inefficient than what I'd like, (and than what is really possible without that nasty bug) but at least it works. It's better than repeating subselects for each project like

and exists (select 1 from project_assignment pa where pa.id = someId and pa.emp_id = e.id)

It consists of performing a self-join subquery in order to find out, for each of the Employees, how many of the projects in the list they are assigned to, and restrict results to only those that are in all of them.

select e 
from Employee
where :listSize = 
    (select distinct count(*)
     from Employee e2, ProjectAssignment pa
     where 
         e2.id = pa.id_emp and 
         e.id = e2.id
         and pa.proj_id IN :projectIdList
    )
Xavi López
  • 27,550
  • 11
  • 97
  • 161