Search code examples
javasqlhibernatesql-order-bycriteria

Criteria Hibernate Order By


I've got two classes with this relation:

Class Articulo:

private List<Poblacion> poblacionList;


Class Poblacion:

private String nombre;

I want to do a query and order by the result by the nombre property:

Criteria criteria = this.getSession().createCriteria(Articulo.class);

I mean i want to do something like...

criteria.addOrder(Order.asc("poblacionList.nombre"));

But this didn't work properly so can i do any trick to do this order?

Sorry for my english and thanks in advice


Solution

  • you need to have a reference to the Poblacion entity.

    Criteria criteria = this.getSession().createCriteria(Articulo.class, "articulo")
                                 .createAlias("articulo.poblacionList", "poblacion");
    
    //... your projections or something else
    
    criteria.addOrder(Order.asc("poblacion.nombre")); //using the alias.