I´m trying to join 3 tables with JPA Critera API and get the result as a list of type other than the relation table.
The Entities are:
| Employee | | Contract | | Company |
|----------| |----------| |---------|
| id | | Company | | id |
| age | | Employee | | name |
- A Contract is the relationship between a Company and Employee
- An employee may belong to one or more Companies
- A company has one or more employees
I try now to get all Employees that work for Company A like so:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Contract> query = cb.createQuery(Contract.class);
Root<Contract> contracts = query.from(Contract.class);
Join<Contract, Company> companyJoin = contracts.join("company");
Join<Contract, Employee> employeeJoin = contracts.join("employee");
List<Predicate> conditions = new ArrayList<Predicate>();
conditions.add(cb.equal(companyJoin.get("name"), "Company A"));
TypedQuery<Practice> typedQuery = em.createQuery(query
.select(contracts)
.where(conditions.toArray(new Predicate[conditions.size()]))
);
typedQuery.getResultList();
This gives me a List of Contracts with Empoyees that work in "Company A".
How can I write the Query to get a List of Employees instead of Contracts?