Caros amigos, estou trabalhando numa manutenção de site que eles querem a ordenação das datas de pagamento, porem esta dando o seguinte erro: Incorrect syntax near the keyword ‘order’. o metodo que montei ficou assim:
public List findListHolerith(String cpf) throws ServiceException{
List<Holerith> listaHolerith = null;
Session hSession = HibernateFactory.factory().getSession();
try{
Criteria hCriteria = hSession.createCriteria(Holerith.class);
hCriteria.add(Restrictions.eq(Holerith.PROP_cpf ,cpf));
hCriteria.add(Restrictions.sqlRestriction("convert(Date,Dtpagamento,103"));
hCriteria.addOrder(Order.desc(Holerith.PROP_dtPagamento));
listaHolerith = hCriteria.list();
} catch(HibernateException e){
log.error(e.getMessage());
throw new ServiceException(e);
} catch(Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e);
}
return listaHolerith;
gostaria da ajuda para entender o que fiz errado. o campo dtpagamento no banco esta string e não pode mudar por isso acrescentei o convert.
Usa SQL nativo. Criteria é masoquismo.
pmlm
Fevereiro 10, 2021, 10:04am
#3
Isto está correto? Abres ( mas nunca fechas…
não entendi, até achei no inicio que eu tinha vacilado em não fechar as aspas… mas esta fechando dentro do parenteses… ou devo colocar aspas simples e as duplas?
pmlm
Fevereiro 10, 2021, 2:27pm
#5
O problema não são as aspas, são os parenteses.
O que tens dentro de aspas é isto
convert(Date,Dtpagamento,103
Bom pessoal desisti do criteria e usei querystring para montar minha consulta. Infelizmente trabalhar em sustentação é isso:
ficou assim:
public List<Holerith> findListHolerith(String cpf) throws ServiceException{
List<Holerith> listaHolerith = new ArrayList<Holerith>();
List<Object[]> listaRetorno = null;
try { Session hSession = HibernateFactory.factory().getSession();
String queryString = "select cpf, dtPagamento from Holerith "
+ "where cpf = :cpf " +
"order by convert(date, dtPagamento, 103)desc " ;
Query hquery = hSession.createQuery(queryString);
hquery.setParameter("cpf", cpf);
listaRetorno = hquery.list();
Holerith objholerit;
for(Object[] obj:listaRetorno){
objholerit = new Holerith();
objholerit.setCpf((String)obj[0]);
objholerit.setDtPagamento((String)obj[1]);
listaHolerith.add(objholerit);
}
} catch(HibernateException e){
log.error(e.getMessage());
throw new ServiceException(e);
} catch(Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e);
}
finally {
HibernateFactory.factory().commitTransaction();
}
return listaHolerith;
}
s
pmlm
Fevereiro 11, 2021, 3:45pm
#7
Experimentaste ao menos corrigir
hCriteria.add(Restrictions.sqlRestriction("convert(Date,Dtpagamento,103"));
para
hCriteria.add(Restrictions.sqlRestriction("convert(Date,Dtpagamento,103)"));
?
sim meu caro eu tentei, e usei até outros exemplos… mas alguem considerou que era algo rapido então tomamos a decisão de fazer aquela solução… obrigado!