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.RecordatoriosDAO; import com.demo.model.Recordatorios; import com.demo.util.HibernateUtil; import com.demo.util.LogSistema; import java.io.Serializable; import java.util.Date; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author ingchristian */ public class RecordatoriosImpl implements RecordatoriosDAO, Serializable { Session session; @Override public boolean guardarRecordatorio(Recordatorios rec) { boolean estado = false; try { session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.save(rec); tx.commit(); estado = true; } catch (HibernateException e) { LogSistema.guardarlog( this.getClass().getName() + " Method: guardarRecordatorios, Exception: " + e.getMessage()); } return estado; } @Override public boolean eliminarRecordatorio(Recordatorios rec) { boolean estado = false; try { session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.delete(rec); tx.commit(); estado = true; } catch (HibernateException e) { LogSistema.guardarlog( this.getClass().getName() + " Method: eliminarRecordatorio, Exception: " + e.getMessage()); } return estado; } @Override public Recordatorios obtenerProxRecordatorio(Date fecha, int clvGestor) { try { Date hoy = new Date(); Date hoymasCinco = new Date(hoy.getTime() + (5 * 60 * 1000)); Session s = HibernateUtil.getSessionFactory().openSession(); Transaction t = s.beginTransaction(); Query q = s.createQuery("from Recordatorios r where r.recordatorioFecha between :fromdate and :todate " + "and r.catGestores.catGestorClv=" + clvGestor + " and r.mostrado='false'"); q.setMaxResults(1); q.setParameter("fromdate", hoy); q.setParameter("todate", hoymasCinco); Recordatorios r = (Recordatorios) q.uniqueResult(); t.commit(); return r; } catch (HibernateException exception) { LogSistema.guardarlog(this.getClass().getName() + " Method: obtenerProxRecordatorio, Exception: " + exception.getMessage()); return null; } } @Override public List<Recordatorios> obtenerProxRecordatorios(Date fecha, int clvGestor) { try { Date hoy = new Date(); //Date hoymasCinco = new Date(hoy.getTime() + (5 * 60 * 1000)); Session s = HibernateUtil.getSessionFactory().openSession(); Transaction t = s.beginTransaction(); Query q = s.createQuery("from Recordatorios r where r.recordatorioFecha>= :fromdate and" + " r.catGestores.catGestorClv=" + clvGestor + " and r.mostrado='false'"); q.setParameter("fromdate", hoy); // q.setParameter("todate", hoymasCinco); List<Recordatorios> r = q.list(); t.commit(); return r; } catch (HibernateException exception) { LogSistema.guardarlog(this.getClass().getName() + " Method: obtenerProxRecordatorio, Exception: " + exception.getMessage()); return null; } } @Override public boolean actualizarRecordatorio(Recordatorios rec) { boolean estado = false; try { session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.update(rec); tx.commit(); estado = true; } catch (HibernateException e) { LogSistema.guardarlog( this.getClass().getName() + " Method: actualizarRecordatorio, Exception: " + e.getMessage()); } return estado; } }