简体   繁体   中英

how to create jpql query to get Entity with no associated parent

I have a unidirectional relationship between an Employee and his Location .The Location is unaware of Employee.

emp1-----------> loc1
emp2------------> loc1
emp3------------> loc3

Using JPA, I mapped this as follows

@Entity
class Employee{
   ...
   @ManyToOne
   Location location;
}
@Entity
class Location{
   ...
   int roomNumber;
   String aptName;
   String street;
   String city;
}

Is it possible to make a jpql query to get a list of all Location objects that have no Employee associated with it.

I think this will work.

select l from Location l where l not in (select e.location from Employee e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM