I have entities structure for keeping building, rooms and floors like this:
Building -- one to many -- Floor -- one to many Room
I'm looking at Criteria API and my goal is to get all rooms first associated with same floor and than to find all rooms associated with same building.
As for first task, I succeeded with following code:
CriteriaQuery<TRoom> criteria = em.getCriteriaBuilder().createQuery(TRoom.class); Root<TRoom> troom = criteria.from(TRoom.class); Predicate condition = em.getCriteriaBuilder().equal(troom.get("tfloor"), em.find(TFloor.class, targetRoom.getFloor().getId())); List<TRoom> trooms = em.createQuery(criteria.where(condition)).getResultList();
I've tried to use the same with different path (eg. "troom.get("tfloor.tbuilding")" or troom.get("tfloor").get("tbuilding")) and with join like this:
Join<TRoom, TFloor> join = troom.join("tfloor"); Predicate condition = em.getCriteriaBuilder().equal(join.get("tbuilding") , em.find(TFloor.class, targetRoom.getFloor().getBuilding().getBuildingId()));
But no avail too. I keep getting empty list or exceptions.
What's the right way to get all rooms for building?