ar.edu.unju.fi.apu.dao.imp.mysql.PacienteDAOImp.java Source code

Java tutorial

Introduction

Here is the source code for ar.edu.unju.fi.apu.dao.imp.mysql.PacienteDAOImp.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 ar.edu.unju.fi.apu.dao.imp.mysql;

import ar.edu.unju.fi.apu.dao.IPacienteDAO;
import ar.edu.unju.fi.apu.hibernate.configuracion.HibernateUtil;
import ar.edu.unju.fi.apu.modelo.dominio.Paciente;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author celso-pc
 */
public class PacienteDAOImp implements IPacienteDAO {

    @Override
    public void crearPaciente(Paciente paciente) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(paciente);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void modificarPaciente(Paciente paciente) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(paciente);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void eliminarPaciente(Paciente paciente) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(paciente);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public List<Paciente> obtenerTodos() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Paciente.class);
        List pacientes = criteria.list();
        return pacientes;

    }

    @Override
    public Paciente obtenerPaciente(int numHC) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Paciente.class);
        criteria.add(Restrictions.eq("numHC", numHC));
        Paciente paciente = (Paciente) criteria.list().get(0);
        return paciente;
    }

}