Example usage for javax.persistence.criteria Subquery correlate

List of usage examples for javax.persistence.criteria Subquery correlate

Introduction

In this page you can find the example usage for javax.persistence.criteria Subquery correlate.

Prototype

<X, K, V> MapJoin<X, K, V> correlate(MapJoin<X, K, V> parentMap);

Source Link

Document

Create a subquery map join object correlated to a map join object of the enclosing query.

Usage

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * subquery(equivalent to dowhere1) : exists
 * SELECT e FROM jpa_query_employee e WHERE EXISTS (SELECT p FROM e.projects p WHERE p.name = :projectname)
 *///from   w  w  w .j  a  v a 2  s  .  co m
@Transactional
public void doWhere2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);

    // subquery
    Subquery<Project> sq = c.subquery(Project.class);
    // Root<Project> p = sq.from(Project.class);
    Root<Employee> se = sq.correlate(e);
    Join<Employee, Project> p = se.join("projects");
    sq.select(p).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname")));

    //
    c.select(e).where(cb.exists(sq));
}