Note that there are some explanatory texts on larger screens.

plurals
  1. POHQL/SQL/Criteria to join-match all records in a given list while selecting all fields
    primarykey
    data
    text
    <p>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 <em>all</em> Projects in order to be selected.</p> <p>An acceptable way of achieving this with native SQL can be found in the answer to this question: <a href="https://stackoverflow.com/q/1849535/851811">T-SQL - How to write query to get records that match ALL records in a many to many join</a>: </p> <pre><code>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] </code></pre> <p>However, I want to select all fields of Employee (<code>e.*</code>). It's not possible to define <a href="https://stackoverflow.com/q/800910/851811">SQL grouping by all the columns</a>(<code>GROUP BY e.*</code>), <code>DISTINCT</code> should be used instead. Is there a way to use <code>DISTINCT</code> altogether with <code>COUNT(*)</code> to achieve what I want?</p> <p>I've also tried using HQL to perform this query. The <code>Employee</code> and <code>ProjectAssignment</code> 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 <a href="https://stackoverflow.com/q/974085/851811">Join without association in HQL</a>. So, my HQL looks like</p> <pre><code>select emp from Employee emp, ProjectAssignment pa where emp.id = pa.empId and pa.paId IN :list group by emp having count(*) = :listSize </code></pre> <p>However, due to a bug in Hibernate, <a href="https://hibernate.onjira.com/browse/HHH-1615" rel="nofollow noreferrer">GROUP BY entity does not work</a>. The SQL it outputs is something like <code>group by (emptable.id)</code>.</p> <p>Subquerying the assignment table for each project (dynamically adding <code>and exists (select 1 from proj_assignment pa where pa.emp_id=e.id and pa.proj_id = [anId])</code> for each project in the list) is not an acceptable option.</p> <p>Is there a way to write this query properly, preferrably in HQL (in the end I want a <code>List&lt;Employee&gt;</code>), without modifying mappings and without explicitly selecting all columns in the native SQL ?</p> <hr> <p><strong>EDIT</strong>: I'm using Oracle 10g and <a href="http://repo1.maven.org/maven2/org/hibernate/hibernate-annotations/3.3.1.GA/hibernate-annotations-3.3.1.GA.pom" rel="nofollow noreferrer">hibernate-annotations-3.3.1.GA</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload