so I have a table called 'User' having fields as
- userId
- userName
- supervisorId
I want to fetch the userName of the supervisor of a particular user.
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
Root<User> rootUser = criteriaQuery.from(User.class);
List<Predicate> predList = new ArrayList<Predicate>();
Join<User, User> selfJoin = rootUser.join("userId", JoinType.LEFT); //not sure about this line
predList.add(criteriaBuilder.equal(selfJoin.<String>get("userId"), supervisorId)); //supervisorId is the id of the supervisor that I want to find
TypedQuery<User> typedQuery = em.createQuery(criteriaQuery);
List<User> resultList = typedQuery.getResultList();
Now I have many other conditions too beside this supervisorId. So I have to use the same criteria query.
Thanks.