data.dao.parent.Dao.java Source code

Java tutorial

Introduction

Here is the source code for data.dao.parent.Dao.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.dao.parent;

import data.entity.parent.PrimEntity;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * ? ?? ? ?? Dao
 *
 * @author Rice Pavel
 */
public abstract class Dao<T extends PrimEntity> {

    public abstract Class getSupportedClass();

    private SessionFactory sessionFactory;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    protected Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    public void save(T obj) {
        currentSession().save(obj);
    }

    public void update(T obj) {
        currentSession().update(obj);
    }

    public List<T> getAllAsc(String... order) {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        for (String ordPosition : order) {
            cr.addOrder(Order.asc(ordPosition));
        }
        cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return cr.list();
    }

    public List<T> getAllDesc(String... order) {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        for (String ordPosition : order) {
            cr.addOrder(Order.desc(ordPosition));
        }
        cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return cr.list();
    }

    public void delete(T obj) {
        currentSession().delete(obj);
    }

    public T getOne(String id) {
        return (T) currentSession().get(getSupportedClass(), Long.valueOf(id));
    }

    public T find(Long id) {
        return (T) currentSession().get(getSupportedClass(), id);
    }

    public List<T> find(T obj) {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.add(Example.create(obj).excludeZeroes());
        return cr.list();
    }

    public List<T> findMax(String name) {
        DetachedCriteria maxId = DetachedCriteria.forClass(getSupportedClass())
                .setProjection(Projections.max(name));
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.add(Property.forName(name).eq(maxId));
        return cr.list();
    }

    public List<T> findMin(String name) {
        DetachedCriteria minId = DetachedCriteria.forClass(getSupportedClass())
                .setProjection(Projections.min(name));
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.add(Property.forName(name).eq(minId));
        return cr.list();
    }

}