segunda-feira, 21 de janeiro de 2008

Criteria Subquery

DetachedCriteria subQuery = DetachedCriteria.forClass(Informacao.class, "subInformacao");
ProjectionList p = Projections.projectionList().create();

p.add(Projections.property("subInformacao.id"), "id");
subQuery.setProjection(p);

Criteria criteria = getSession().createCriteria(Aluno.class, "a");

ProjectionList p2 = Projections.projectionList().create();

p2.add(Projections.property("a.id"), "id");
p2.add(Projections.property("a.nome"), "nome");

criteria.setProjection(p2);
criteria.add(Property.forName("a.id").in(subQuery));

criteria.setResultTransformer(new AliasToBeanResultTransformer(Aluno.class));
Collection alunos = criteria.list();

System.out.println(alunos.size());

// CONSOLE:
// select
// this_.id as y0_,
// this_.nome as y1_
// from
// Aluno this_
// where
// this_.id in (
// select
// this0__.id as y0_
// from
// Informacao this0__
// )

Nenhum comentário: