Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
865 B
1
Indexable
Never
public Employee findEmployeeByTelNumber(EntityManager entityManager, String telNumber) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
    
    Root<PhoneNumber> phoneNumberRoot = criteriaQuery.from(PhoneNumber.class);
    criteriaQuery.select(phoneNumberRoot.get("address").get("employee"));
    criteriaQuery.where(criteriaBuilder.equal(phoneNumberRoot.get("telNumber"), telNumber));
    
    TypedQuery<Employee> query = entityManager.createQuery(criteriaQuery);
    List<Employee> employees = query.getResultList();
    
    if (!employees.isEmpty()) {
        return employees.get(0);
    } else {
        return null;
    }
}
This method works similarly to the previous example, but it starts by selecting the employee attrib