com.catexpress.dao.impl.GenericDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.catexpress.dao.impl.GenericDaoImpl.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 com.catexpress.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.catexpress.dao.GenericDAO;
import com.catexpress.model.BaseEntity;
import com.catexpress.util.HibernateUtil;
import com.catexpress.util.Tupla;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author rafael
 * @param <T>
 * @param <Id>
 */
public class GenericDaoImpl<T extends BaseEntity, Id extends Serializable> implements GenericDAO<T, Id> {

    protected Class<T> clase;
    protected Session s;
    protected Transaction t;

    public GenericDaoImpl(Class<T> clase) {
        this.clase = clase;
    }

    @Override
    public Id save(T obj) {
        Id i = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            i = (Id) s.save(obj);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return i;
    }

    @Override
    public void update(T obj) {
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            s.update(obj);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
    }

    @Override
    public void delete(T obj) {
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            s.delete(obj);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
    }

    @Override
    public boolean contains(T obj) {
        boolean b = false;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            b = s.contains(obj);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return b;
    }

    @Override
    public List<T> findAll() {
        List<T> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Criteria c;
            c = s.createCriteria(clase);
            l = c.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    @Override
    public List<T> findBy(String field, Object value, Boolean isString) {
        List<T> list = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Criteria criteria = s.createCriteria(clase);
            if (isString) {
                criteria.add(Restrictions.like(field, "%" + value.toString() + "%"));
            } else {
                criteria.add(Restrictions.eq(field, value));
            }
            list = criteria.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return list;

    }

    @Override
    public T findById(Id id) {
        T d = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            d = (T) s.get(clase, id);
            t.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

    @Override
    public List<T> executeQuery(String jpql, Object... values) {
        List<T> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createQuery(jpql);
            int i = 1;
            for (Object object : values) {
                q.setParameter(i, object);
                i++;
            }
            l = (List<T>) q.list();
            t.commit();

        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    @Override
    public List<T> executeQuery(String jpql, Tupla... params) {
        List<T> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createQuery(jpql);
            for (Tupla tupla : params) {
                q.setParameter(tupla.getLlave(), tupla.getParametro());
            }
            l = (List<T>) q.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    /**
     *
     * @param jpql
     * @param mapa
     * @param maximoResultados
     * @return
     */
    public List<T> executeQuery(String jpql, Map<String, Object> mapa, Integer maximoResultados) {
        List<T> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createQuery(jpql);
            for (Map.Entry<String, Object> entry : mapa.entrySet()) {
                q.setParameter(entry.getKey(), entry.getValue());
            }
            if (maximoResultados == null) {
                l = (List<T>) q.list();
            } else {
                l = (List<T>) q.setMaxResults(maximoResultados).list();
            }
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    @Override
    public List<T> executeQuery(String jpql, Map<String, Object> mapa, boolean isNew) {
        List<T> l = null;
        StringBuilder query = new StringBuilder(jpql);
        int i = 0;
        for (Map.Entry<String, Object> entry : mapa.entrySet()) {
            String campo = entry.getKey();
            if (isNew && i == 0) {
                query.append(" where ");
            }
            if (i > 0) {
                query.append(" and ");
            }
            query.append(campo);
            if (entry.getValue() instanceof String) {
                query.append(" like :").append(campo.replace(".", ""));
            } else {
                query.append(" = :").append(campo.replace(".", ""));
            }
            i++;
        }
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createQuery(query.toString());
            for (Map.Entry<String, Object> entry : mapa.entrySet()) {
                if (entry.getValue() instanceof String)
                    q.setParameter(entry.getKey().replace(".", ""), "%" + entry.getValue() + "%");
                else
                    q.setParameter(entry.getKey().replace(".", ""), entry.getValue());
            }
            l = (List<T>) q.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    /**
     * {@inheritDoc}
     */
    public List<Object[]> executeNativeQuery(String jpql) {
        List<Object[]> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createSQLQuery(jpql);
            l = q.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    public List<Object[]> executeNativeQuery(String jpql, Object... values) {
        List<Object[]> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createSQLQuery(jpql);
            int i = 1;
            for (Object object : values) {
                q.setParameter(i, object);
                i++;
            }
            l = q.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    public List<T> executeQuery(int max, String jpql, Object... values) {
        List<T> l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createQuery(jpql);
            int i = 1;
            for (Object object : values) {
                q.setParameter(i, object);
                i++;
            }
            q.setMaxResults(max);
            l = (List<T>) q.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    /*public T executeSingleQuery(String jpql, Object... values) {
    Query q = entityManager.createQuery(jpql);
    int i = 1;
    for (Object object : values) {
        q.setParameter(i, object);
        i++;
    }
    T result = (T) q.getSingleResult();
    return result;
    }
        
    /**
     * {@inheritDoc}
     */
    @Override
    public List executeQueryObject(String jpql, Object... values) {
        List l = null;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createQuery(jpql);
            int i = 1;
            for (Object object : values) {
                q.setParameter(i, object);
                i++;
            }
            l = q.list();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }

    @Override
    public Integer nativeUpdate(String sql, Map<String, Object> mapa) {
        Integer l;
        try {
            s = HibernateUtil.getSessionFactory().getCurrentSession();
            t = s.beginTransaction();
            Query q = s.createSQLQuery(sql);
            for (Map.Entry<String, Object> entry : mapa.entrySet()) {
                q.setParameter(entry.getKey(), entry.getValue());
            }
            l = q.executeUpdate();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            throw e;
        }
        return l;
    }
    /*public List executeQueryObject(String jpql, Map<String, Object> mapa) {
    Query q = entityManager.createQuery(jpql);
        
    for (Map.Entry<String, Object> entry : mapa.entrySet()) {
        q.setParameter(entry.getKey(), entry.getValue());
    }
        
    List result = q.getResultList();
    return result;
    }*/

    @Override
    public List<T> likeNombre(String like) {
        return null;
    }

}