Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The method for utilizing the same association multiple times in HQL is by using aliases or joining the association multiple times with different aliases.

Example 1: Using aliases

FROM Employee e JOIN e.department d1 JOIN e.department d2 WHERE d1.name = 'Sales' AND d2.name = 'Marketing'

In this example, we are using aliases d1 and d2 to join the Employee entity with the Department entity twice to get employees who belong to both the Sales and Marketing departments.

Example 2: Joining multiple times

FROM Employee e JOIN e.department d WHERE e.salary > d.maxSalary AND e.salary > d.minSalary

In this example, we are joining the Employee entity with the Department entity twice, using the same association e.department both times, with a different condition each time. This query returns employees whose salary is between the minimum and maximum salary of their department.