tags:

views:

351

answers:

1

Simple JPA/JPQL question. I have an entity with a ManyToMany relationship:

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

What is the JPQL query to return all the Employees that do not have any projects?

+2  A: 
from Employee e where not exists elements(e.projects)

or

from Employee e where size(e.projects) = 0
ChssPly76
Hmmmm. I get an error with the first ("Exception Description: Syntax error parsing the query..." using Eclipselink), but the second seems to work as advertised.
Tim
The first is invalid JPQL ... EXISTS requires a subquery to follow
DataNucleus
@Andy - you're right, `elements()` doesn't seem to be in JPA spec, it's a Hibernate extension.
ChssPly76