How do I use Hibernate’s Restriction.in criterion?

This example demonstrate the use of Hibernate’s Restriction.in criterion. This restriction will query for some record based on a collection of parameter defined for a specific property of the bean.

package org.kodejava.example.hibernate.criteria;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.cfg.AnnotationConfiguration;
import org.kodejava.example.hibernate.model.Genre;

import java.util.List;

public class RestrictionInDemo {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().
                    configure("hibernate.cfg.xml").
                    buildSessionFactory();
        }
        catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

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

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        final Session session = getSession();
        try {
            //
            // Create a Criteria an add an in constraint for the property id.
            // This in restrictions will return genre with id 1, 2, 3 and 4.
            //
            Criteria criteria = session.createCriteria(Genre.class)
                    .add(Restrictions.in("id", new Long[] {1l, 2l, 3l, 4l}));

            List result = criteria.list();

            for (Genre genre : result) {
                System.out.println(genre.getId() + "; " + genre.getName());
            }
        } finally {
            session.close();
        }
    }
}

How do I use min, max, avg and sum Projections?

The code below demonstration the use of Projections.min(), Projections.max(), Projections.avg() and Projections.sum().

package org.kodejava.example.hibernate.criteria;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.cfg.AnnotationConfiguration;
import org.kodejava.example.hibernate.model.Track;

public class MinMaxAvgSumProjectionsDemo {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().
                    configure("hibernate.cfg.xml").
                    buildSessionFactory();
        }
        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)
                    .setProjection(Projections.max("duration"));
            Integer maxDuration = (Integer) criteria.uniqueResult();
            System.out.println("Max Track Duration = " + maxDuration);

            criteria.setProjection(Projections.min("duration"));
            Integer minDuration = (Integer) criteria.uniqueResult();
            System.out.println("Min Track Duration = " + minDuration);
            
            criteria.setProjection(Projections.avg("duration"));
            Double avgDuration = (Double) criteria.uniqueResult();
            System.out.println("Avg Track Duration = " + avgDuration);
            
            criteria.setProjection(Projections.sum("duration"));
            Integer totalDuration = (Integer) criteria.uniqueResult();
            System.out.println("Total Track Duration = " + totalDuration);
        } finally {
            session.close();
        }
    }
}