Duvida para listar no postgre

2 respostas
U

Como faço para listar os últimos N registros de uma tabela T ordenados em ordem decrescente? estou utilizando Postgre e hibernate.

Ex:

1
2
3
4
5
6
7
8
9
10

Ou ultimos 3 decrescente seriam
10
9
8

2 Respostas

Mero_Aprendiz

ununes:
Como faço para listar os últimos N registros de uma tabela T ordenados em ordem decrescente? estou utilizando Postgre e hibernate.

Ex:

1
2
3
4
5
6
7
8
9
10

Ou ultimos 3 decrescente seriam
10
9
8

Olá…
Acho que deseja o Limit (http://www.postgresql.org/docs/8.1/static/queries-limit.html) usado junto com o order by desc.
Abraços.

U

Obrigado, sua dica ajudou, abaixo segue a implementação que funcionou.

public Collection<?> listTabelaOrdenado(String column, int ordem, int N)
{	    
   List resultado = null;
   Session session = null;
	    
   try 
   {
	session = HibernateUtil.getSession();
	String order = "ASC";			
        if (ordem != 0)
	   order = "DESC";
			
	resultado = session.createQuery("from Cliente cliente order by " + column + " " + order).setMaxResults(N).list();
	HibernateUtil.closeSession();
   } 
   catch (HibernateException ex) 
   {			
      throw new PersistenceException(ex);
   }			
   return resultado;			   
}
Criado 2 de março de 2009
Ultima resposta 2 de mar. de 2009
Respostas 2
Participantes 2