java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to order the Criteria query resultset?

In this demo you'll see how to order the resultset of out Criteria query. It can be done by adding the org.hibernate.criterion.Order into the Criteria object and we can either order the result in ascending or descending order.

package com.javacoderanch.example.hibernate.criteria;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.cfg.AnnotationConfiguration;
import com.javacoderanch.example.hibernate.model.Track;

import java.util.List;

public class CriteriaOrderDemo {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().
        catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);

    public static Session getSession() throws HibernateException {
        return sessionFactory.openSession();

    public static void main(String[] args) {
        final Session session = getSession();
        try {
            Criteria criteria = session.createCriteria(Track.class)

            List<Track> tracks = criteria.list();
            for (Track track : tracks) {
                System.out.println("Track = " + track.getTitle());
        } finally {