Java tutorial
/* * 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.demo.impl; import com.demo.dao.TareasDao; import com.demo.model.Tarea; import com.demo.util.HibernateUtil; import com.demo.util.LogSistema; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author ivan */ public class TareasImpl implements TareasDao, Serializable { @Override public boolean altaTarea(Tarea t) { Session sess = null; try { sess = HibernateUtil.getSessionFactory().openSession(); Transaction tx = sess.beginTransaction(); //LogSistema.guardarlog("antes de update pago"); sess.save(t); tx.commit(); if (sess.isOpen()) { sess.close(); } return true; } catch (HibernateException he) { LogSistema.guardarlog(this.getClass().getName() + " Method: altaTarea, Exception: " + he.getMessage()); return false; } } @Override public boolean actuTarea(Tarea t) { Session sess = null; try { sess = HibernateUtil.getSessionFactory().openSession(); Transaction tx = sess.beginTransaction(); //LogSistema.guardarlog("antes de update pago"); sess.update(t); tx.commit(); if (sess.isOpen()) { sess.close(); } return true; } catch (HibernateException he) { LogSistema.guardarlog(this.getClass().getName() + " Method: actuTarea, Exception: " + he.getMessage()); return false; } } @Override public List<Tarea> getTareas() { try { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction t = session.beginTransaction(); List<Tarea> lista = session.createQuery("from Tarea t where t.activa=true ").list(); t.commit(); if (session.isOpen()) { session.close(); } return lista; } catch (HibernateException he) { LogSistema.guardarlog(this.getClass().getName() + " Method: getTareas, Exception: " + he.getMessage()); List<Tarea> lista = new ArrayList<>(); return lista; } } @Override public List<Tarea> getTareas(int gestor) { Session session; Transaction t; List<Tarea> retorno = new ArrayList<>(); try { session = HibernateUtil.getSessionFactory().openSession(); t = session.beginTransaction(); List<Tarea> lista = session.createSQLQuery("select t.* from Tarea t, Credito_SF_LT_NT_CT cl where\n" + "t.Credito_SF_LT_NT_CT_CreditosII=cl.CreditosII and cl.Cat_Gestores_Cat_Gestor_clv=" + gestor + " and\n" + "t.Activa=true;").addEntity(Tarea.class).list(); t.commit(); retorno.addAll(lista); session = HibernateUtil.getSessionFactory().openSession(); t = session.beginTransaction(); lista = session.createSQLQuery("select t.* from Tarea t, Credito_Auto ca where\n" + "t.Credito_Auto_CreditoAut=ca.CreditoAut and ca.Cat_Gestores_Cat_Gestor_clv=" + gestor + " and\n" + "t.Activa=true;").addEntity(Tarea.class).list(); t.commit(); retorno.addAll(lista); session = HibernateUtil.getSessionFactory().openSession(); t = session.beginTransaction(); lista = session.createSQLQuery("select t.* from Tarea t, Credito_Fianzas cf where\n" + "t.Credito_Fianzas_NumContratoFZ=cf.NumContratoFZ and cf.Cat_Gestores_Cat_Gestor_clv=" + gestor + " and\n" + "t.Activa=true;").addEntity(Tarea.class).list(); t.commit(); retorno.addAll(lista); session = HibernateUtil.getSessionFactory().openSession(); t = session.beginTransaction(); lista = session.createSQLQuery("select t.* from Tarea t, Credito_Hipotecario ch where\n" + "t.Credito_Hipotecario_CreditoHip=ch.CreditoHip and ch.Cat_Gestores_Cat_Gestor_clv=" + gestor + " and\n" + "t.Activa=true;").addEntity(Tarea.class).list(); t.commit(); retorno.addAll(lista); if (session.isOpen()) { session.close(); } return retorno; } catch (HibernateException he) { LogSistema.guardarlog(this.getClass().getName() + " Method: getTareas, Exception: " + he.getMessage()); List<Tarea> lista = new ArrayList<>(); return lista; } } @Override public List<Tarea> getTareas(String credito, int tipoCredito) { Session session; Transaction t; List<Tarea> retorno = new ArrayList<>(); try { session = HibernateUtil.getSessionFactory().openSession(); t = session.beginTransaction(); switch (tipoCredito) { case 1: break; case 2: break; case 3: break; case 4: retorno = session.createSQLQuery("select t.* from Tarea t where\n" + "t.Credito_SF_LT_NT_CT_CreditosII=" + credito + " and\n" + "t.Activa=true;") .addEntity(Tarea.class).list(); t.commit(); break; } return retorno; } catch (HibernateException he) { LogSistema.guardarlog(this.getClass().getName() + " Method: getTareas, Exception: " + he.getMessage()); List<Tarea> lista = new ArrayList<>(); return lista; } } }