The tutorials over how to configure Hibernate with Spring using XML or Annotation based configuration, where about configuring hibernate with spring. This tutorials is pure over the CRUD operations with the Hibernate Criteria Query API.




Hibernate Criteria Query API

package org.camelcode.repository.impl;

import org.camelcode.model.Book;
import org.camelcode.repository.BookRepository;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository("bookRepository")
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public class BookRepositoryImpl implements BookRepository {

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    public List getAll() {
        return getSession().createCriteria(Book.class).list();
    }

    public Book getById(Integer id) {
        return (Book) getSession().get(Book.class, id);
    }

    @Transactional(readOnly = false)
    public Book saveOrUpdate(Book book) {
        if (book.getId() != null){
            getSession().merge(book);
        } else {
            getSession().save(book);
        }
        return book;
    }

    @Transactional(readOnly = false)
    public void delete(Book book) {
        getSession().delete(book);
    }

    public Integer count() {
        return (Integer) getSession().createCriteria(Book.class)
                .setProjection(Projections.rowCount())
                .uniqueResult();
    }

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }
}