I want to select all entities that are tied by a custom hierarchy:
@Entity
class Employee {
@Id
private long id;
@ManyToOne
private Company company;
}
@Entity
class Company {
@Id
private long id;
@ManyToOne
private LeisureGroup leisureGroup;
}
@Entity
class LeisureGroup {
@Id
private long id;
}
//select all companies that belong to a LeisureGroup
Select * from company where leisureGroupId = '123'
TODO: how can I select all employees that belong to the LeisureGroup (tied by the Company reference)? Do I have to use joins
here? If yes, how?
You don't use SQL if you want to query through JPA, you use JPQL (Java Persistence Query Language) or HQL (Hibernate Query Language) (for Hibernate users).
JPQL query (requires an
EntityManager
variable calledem
)SQL equivalent: