Hibernate的Criteria的一些实用case

简单的查询

SQL


 select * from A where a.p1 = 'p1' and a.p2 = 'p2' order by p.p3 asc

A是一个JPA entity

Criteria


Criteria criteria = createCriteria(A.class)
        .add(Restrictions.eq("p1","p1")
        .add(Restrictions.eq("p2","p2")
        .addOrder(Order.asc("p3"));

List listA = crieria.list();

SQL


 select p1, p2 from A where a.p1 = 'p1' and a.p2 = 'p2' order by p.p3 asc

A是一个JPA entity

Criteria


Criteria criteria = createCriteria(A.class)
        .add(Restrictions.eq("p1","p1")
        .add(Restrictions.eq("p2","p2")
        .addOrder(Order.asc("p3"));
criteria.setProjection(
                Projections.projectionList()
                        .add(Projections.property("a.p1"))
                        .add(Projections.property("a.p2")));

List listA = crieria.list();

Hibernate提供了Projection来从执行的查询中选择查处的对象的properties

查询结果转化一个新的bean

将从entity A中查出的结果转换成bean B


Criteria criteria = createCriteria(A.class,  a)
        .add(Restrictions.eq("p1","p1")
        .add(Restrictions.eq("p2","p2")
        .addOrder(Order.asc("p3"));

criteria.setProjection(
                Projections.projectionList()
                        .add(Projections.property("a.p1"), "p1")
                        .add(Projections.property("a.p2"), "p2"))
                .setResultTransformer(Transformers.aliasToBean(B.class));

List listB = crieria.list();

count行数

SQL


 select count(*) from A where a.p1 = 'p1' and a.p2 = 'p2' order by p.p3 asc

Criteria


Criteria criteria = createCriteria(A.class)
        .add(Restrictions.eq("p1","p1")
        .add(Restrictions.eq("p2","p2")
        .addOrder(Order.asc("p3"));

Long counts = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();

集合

SQL


 select max(p1) from A where a.p3 = 'p3'  group by p2 order by p.p4 asc

Criteria


Criteria criteria = createCriteria(A.class)
        .add(Restrictions.eq("p3","p3")
        .addOrder(Order.asc("p4"));

criteria.setProjection(
                Projections.projectionList()
                  .add(Projections.groupProperty("p2"))
                  .add(Projections.max("p1")))

Long counts = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();

子查询

在实际的项目中遇到这样一个sql


 select a1.p1, a2.p2 from A a1 join (select max(p3) as p3, p4 from A group by p4) a2 
     on a1.p3 = a2.p3 and a1.p4 = a2.p4 where a.p5 = 'p5'

由于这个sql也可以写成下面这样


 select a1.p1, a2.p2 from A a1 where p3 =
         (select max(p3) from A a2 where a1.p4 = a2.p4 group by a2.p4)
       and p5 = 'p5'

所以可用实用criteria的子查询来实现

Criteria


DetachedCriteria innerCriteria = DetachedCriteria.forClass(A.class, "inner")
                .add(Restrictions.eqProperty("inner.p4", "out.p4"))
                .setProjection(Projections.projectionList().add(Projections.max("inner.p3")));
Criteria outerCriteria = createCriteria(WhiteLabelTemplate.class, "out")
                .add(Subqueries.propertyEq("out.p3", innerCriteria))
                .add(Restrictions.eq("p5", "p5"));

result = outerCriteria.list();

你可能感兴趣的:(Hibernate的Criteria的一些实用case)