JPQL for entities without elements in ManyToMany relationships

A simple JPA / JPQL question. I have an entity with ManyToMany relation:

@Entity
public class Employee {      
  @ManyToMany
  @JoinTablename="employee_project"
      joinColumns={@JoinColumn(name="employee_id"}
      inverseJoinColumns={@JoinColumn(name="project_id"})
  private List<Project> projects;

What is a JPQL query to return all employees who have no projects?

+3
source share
2 answers
from Employee e where not exists elements(e.projects)

or

from Employee e where size(e.projects) = 0
+5
source

JQPL has a dedicated IS [NOT] EMPTYcomparison operator for validation; it is an empty collection:

SELECT e FROM Employee e WHERE e.projects IS EMPTY
+2
source

Source: https://habr.com/ru/post/1719454/


All Articles