Example usage for javax.persistence.criteria Root type

List of usage examples for javax.persistence.criteria Root type

Introduction

In this page you can find the example usage for javax.persistence.criteria Root type.

Prototype

Expression<Class<? extends X>> type();

Source Link

Document

Create an expression corresponding to the type of the path.

Usage

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

/**
 * NOTE : Hibernate cannot support case in criteria way
 * //from w  w  w.j av a 2  s.c  o m
 * case 1:
 * SELECT p.name, 
 *          CASE WHEN TYPE(p)=bq.jpa.demo.query.domain.DesignProject  THEN 'dev' 
 *              WHEN TYPE(p)=bq.jpa.demo.query.domain.QualityProject THEN 'QA' 
 *              ELSE 'unknown' 
 *          END 
 * FROM jpa_query_project p
 * 
 * case 2:
 * SELECT p.name, 
 *          CASE TYPE(p)
 *              WHEN bq.jpa.demo.query.domain.DesignProject  THEN 'dev' 
 *              WHEN bq.jpa.demo.query.domain.QualityProject THEN 'QA' 
 *              ELSE 'unknown' 
 *          END 
 * FROM jpa_query_project p
 */
@Transactional
public void doWhere4() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
    Root<Project> p = c.from(Project.class);

    // method 1
    c.multiselect(p.get("name"), cb.selectCase().when(cb.equal(p.type(), DesignProject.class), "dev")
            .when(cb.equal(p.type(), QualityProject.class), "QA").otherwise("unknown"));

    //      showResult(c);

    // method 2:
    CriteriaQuery<Object[]> c2 = cb.createQuery(Object[].class);
    Root<Project> p2 = c2.from(Project.class);
    c2.multiselect(p2.get("name"), cb.selectCase(p2.type()).when(DesignProject.class, "dev")
            .when(QualityProject.class, "QA").otherwise("unknown"));
    //      showResult(c2);
}