025. Criteria Query - dkkahm/study-spring-jpa GitHub Wiki

Simple Steps

  1. Use Criteria Builder to create a Criteria Query returning the expected result object
  2. Define roots for tables which are involved in the query
  3. Define Predicates etc using Criteria Builder
  4. Add Predicates etc to the Criteria Query
  5. Builder the TypedQuery using the entity manager and criteria query

Select all courses (Select c From Course c)

        // 1.
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Course> cq = cb.createQuery(Course.class);

        // 2.
        Root<Course> courseRoot = cq.from(Course.class);

        // 3.
        // 4.

        // 5.
        TypedQuery<Course> query = em.createQuery(cq.select(courseRoot));

        List<Course> resultList = query.getResultList();

        logger.info("{}", resultList);

Select course having patter (Select c From Course c where name like '%Spring%')

        // 1.
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Course> cq = cb.createQuery(Course.class);

        // 2.
        Root<Course> courseRoot = cq.from(Course.class);

        // 3.
        Predicate likeSpring = cb.like(courseRoot.get("name"), "%Spring%");
        cq.where(likeSpring);

        // 4.

        // 5.
        TypedQuery<Course> query = em.createQuery(cq.select(courseRoot));

        List<Course> resultList = query.getResultList();

        logger.info("{}", resultList);

All courses without students (Select c From Course c where c.students is empty)

        // 1.
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Course> cq = cb.createQuery(Course.class);

        // 2.
        Root<Course> courseRoot = cq.from(Course.class);

        // 3.
        Predicate studentsIsEmpty = cb.isEmpty(courseRoot.get("students"));
        cq.where(studentsIsEmpty);

        // 4.

        // 5.
        TypedQuery<Course> query = em.createQuery(cq.select(courseRoot));

        List<Course> resultList = query.getResultList();

        logger.info("{}", resultList);

Join (Select c From Course c JOIN c.students)

        // 1.
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Course> cq = cb.createQuery(Course.class);

        // 2.
        Root<Course> courseRoot = cq.from(Course.class);

        // 3.
        Join<Object, Object> join = courseRoot.join("students");

        // 4.

        // 5.
        TypedQuery<Course> query = em.createQuery(cq.select(courseRoot));

        List<Course> resultList = query.getResultList();

        logger.info("{}", resultList);

LeftJoin (Select c From Course c LEFT JOIN c.students)

        // 1.
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Course> cq = cb.createQuery(Course.class);

        // 2.
        Root<Course> courseRoot = cq.from(Course.class);

        // 3.
        Join<Object, Object> join = courseRoot.join("students", JoinType.LEFT);

        // 4.

        // 5.
        TypedQuery<Course> query = em.createQuery(cq.select(courseRoot));

        List<Course> resultList = query.getResultList();

        logger.info("{}", resultList);
⚠️ **GitHub.com Fallback** ⚠️