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.Roles; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author esteban */ public class Roles_Model { public void addRol(Roles 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 deleteRol(int objid) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Roles obj = (Roles) session.load(Roles.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 updateRol(Roles 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<Roles> getAllRoles() { List<Roles> obj = new ArrayList<Roles>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); obj = session.createQuery("from Roles").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; } public List<Roles> listNoActive() { List<Roles> obj = new ArrayList<Roles>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); obj = session.createQuery("from Roles").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; } public Object getRolesById(Integer id) { Object obj = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Roles " + " where Id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", id); obj = query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return obj; } }