在此演示中,您将看到如何对outCriteria查询的结果集进行排序。可以通过org.hibernate.criterion.Order在Criteria对象中添加来完成,我们可以按升序或降序对结果进行排序。
package org.nhooo.example.hibernate.criteria; import org.hibernate.SessionFactory; import org.hibernate.Session; import org.hibernate.HibernateException; import org.hibernate.Criteria; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; import org.nhooo.example.hibernate.model.Track; import java.util.List; public class CriteriaOrderDemo { public static Session getSession() throws HibernateException { String cfg = "hibernate.cfg.xml"; SessionFactory sessionFactory = new Configuration().configure(cfg) .buildSessionFactory(); return sessionFactory.openSession(); } @SuppressWarnings("unchecked") public static void main(String[] args) { try (Session session = getSession()) { Criteria criteria = session.createCriteria(Track.class) .addOrder(Order.asc("title")) .setFirstResult(0) .setMaxResults(10); List<Track> tracks = criteria.list(); for (Track track : tracks) { System.out.println("Track = " + track.getTitle()); } } } }