Datos.ProcedimientoDAO.java Source code

Java tutorial

Introduction

Here is the source code for Datos.ProcedimientoDAO.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 Datos;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import Entidades.Procedimiento;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author ernesto
 */
public class ProcedimientoDAO {
    private Session sesion;
    private Transaction tx;

    private void iniciarOperacion() throws HibernateException {
        sesion = HibernateUtil.getSessionFactory().openSession();
        tx = sesion.beginTransaction();
    }

    private void manejaExcepcion(HibernateException he) throws HibernateException {
        tx.rollback();
        throw new HibernateException("Ocurri un error en la capa de acceso a datos", he);
    }

    public long insertar(Procedimiento procedimiento) throws HibernateException {
        long id = 0;

        try {
            iniciarOperacion();
            id = (long) sesion.save(procedimiento);
            tx.commit();
        } catch (HibernateException he) {
            manejaExcepcion(he);
        } finally {
            sesion.close();
        }

        return id;
    }

    public void modificar(Procedimiento procedimiento) throws HibernateException {
        try {
            iniciarOperacion();
            sesion.update(procedimiento);
            tx.commit();
        } catch (HibernateException he) {
            manejaExcepcion(he);
        } finally {
            sesion.close();
        }
    }

    public void eliminar(Procedimiento procedimiento) throws HibernateException {
        try {
            iniciarOperacion();
            sesion.delete(procedimiento);
            tx.commit();
        } catch (HibernateException he) {
            manejaExcepcion(he);
        } finally {
            sesion.close();
        }
    }

    public Procedimiento obtener(long id) throws HibernateException {
        Procedimiento procedimiento = null;

        try {
            iniciarOperacion();
            procedimiento = (Procedimiento) sesion.get(Procedimiento.class, id);

        } catch (HibernateException he) {
            manejaExcepcion(he);
        } finally {
            sesion.close();
        }

        return procedimiento;
    }

    public List<Procedimiento> obtenerTodos() throws HibernateException {
        List<Procedimiento> listaCubiculos = null;

        try {
            iniciarOperacion();
            listaCubiculos = sesion.createQuery("from Procedimiento").list();
        } finally {
            sesion.close();
        }

        return listaCubiculos;
    }

    public List<Procedimiento> obtenerTodosPorFecha(Date fechaInicial, Date fechaFinal) throws HibernateException {
        List<Procedimiento> listaCubiculos = null;

        try {
            iniciarOperacion();
            Query query = sesion
                    .createQuery("from Procedimiento where fecha between :fechaInicial and :fechaFinal");
            query.setDate("fechaInicial", fechaInicial);
            query.setDate("fechaFinal", fechaFinal);

            listaCubiculos = query.list();
        } finally {
            sesion.close();
        }

        return listaCubiculos;
    }
}