1

好吧,我有以下问题,通过设置昵称(alias)来进行表之间的join,我需要做一个decode,使用了alias 别名,但是使用了因为它不识别使用纯sql。

如何返回定义表条件的名称?我正在使用 sqlGroupProjection,如果你能建议另一种方式。

Criteria criteria = dao.getSessao().createCriteria(Chamado.class,"c");

criteria.createAlias("c.tramites","t").setFetchMode("t", FetchMode.JOIN);

projetos.add( Projections.rowCount(),"qtd");

criteria.add(Restrictions.between("t.dataAbertura", Formata.getDataD(dataInicio, "dd/MM/yyyy"), Formata.getDataD(dataFim, "dd/MM/yyyy")));

projetos.add(Projections.sqlGroupProjection("decode(t.cod_estado, 0, 0, 1, 1, 2, 1, 3, 2, 4, 1, 5, 3)  as COD_ESTADO",
             "decode(t.cod_estado, 0, 0, 1, 1, 2, 1, 3, 2, 4, 1, 5, 3)", 
             new String[]{"COD_ESTADO"}, 
             new Type[]{Hibernate.INTEGER}));

criteria.setProjection(projetos);

List<Relatorio> relatorios = criteria.setResultTransformer(Transformers.aliasToBean(Relatorio.class)).list();

按条件生成的 SQL:

select count(*) as y0_, 

       decode(t.cod_estado, 0, 0, 1, 1, 2, 1, 3, 2, 4, 1, 5, 3) as COD_ESTADO 

from CHAMADOS this_  

 inner join TRAMITES t1_ on this_.COD_CHAMADO = t1_.COD_CHAMADO  

where t1_.DT_ABERTURA between ? and ?  

group by decode(t.cod_estado, 0, 0, 1, 1, 2, 1, 3, 2, 4, 1, 5, 3)
4

0 回答 0