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.esteban.cmms.maven.model; import com.esteban.cmms.maven.controller.beans.Documentos; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author esteban */ public class Documentos_Model { public void addDocumento(Documentos obj) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.save(obj); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void deleteDocumento(int objid) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Documentos obj = (Documentos) session.load(Documentos.class, new Integer(objid)); session.delete(obj); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void updateDocumento(Documentos obj) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.update(obj); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public List<Documentos> getAllDocumentos() { List<Documentos> obj = new ArrayList<Documentos>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); obj = session.createQuery("from Documentos").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; } public List<Documentos> listNoActive() { List<Documentos> obj = new ArrayList<Documentos>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); obj = session.createQuery("from Documentos" + " where Estado='Inactivo'").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; } public Documentos getDocumentoById(int objid) { Documentos obj = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Documentos where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", objid); obj = (Documentos) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; } }