public void add(Maintenance maintenance) { try { session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); session.save(maintenance); session.getTransaction().commit(); } catch (Exception e) { System.err.println("Can`t add object to database\n " + e); } finally { if (session != null) session.close(); } }
public List<Maintenance> getAll() { List<Maintenance> result = null; try { session = HibernateUtil.getSessionFactory().openSession(); Criteria criteria = session.createCriteria(Maintenance.class); result = (List<Maintenance>) criteria.list(); session.getTransaction().commit(); } catch (Exception e) { System.err.println("Error in getting information from \"Capital\"\n" + e); } finally { if (session != null) session.close(); } return result; }
public void delete(int id) { try { session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Maintenance capital = (Maintenance) session.get(Maintenance.class, id); if (capital == null) { throw new NullPointerException(); } session.delete(capital); session.getTransaction().commit(); } catch (NullPointerException e) { System.err.println("There are not any item with this id\n" + e); } catch (Exception e) { System.err.println("Can`t delete this item\n" + e); } finally { if (session != null) session.close(); } }
public void update(Maintenance maintenance, int id) { try { session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Maintenance updateItem = (Maintenance) session.get(Maintenance.class, id); if (updateItem == null) throw new NullPointerException(); updateItem.setDate(maintenance.getDate()); updateItem.setNumberEquipment(maintenance.getNumberEquipment()); updateItem.setKindMaintenance(maintenance.getKindMaintenance()); session.update(updateItem); session.getTransaction().commit(); } catch (NullPointerException e) { System.err.println("Can`t find item with this id\n" + e); } catch (Exception e) { System.err.println("Can`t update this item\n" + e); } finally { if (session != null) session.close(); } }
public Maintenance get(int id) { Maintenance findItem = new Maintenance(); try { session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); findItem = (Maintenance) session.get(Maintenance.class, id); if (findItem == null) { throw new NullPointerException(); } session.getTransaction().commit(); } catch (NullPointerException e) { System.err.println("There are not any item with this id"); findItem = new Maintenance(); } catch (Exception e) { System.err.println("Can`t find this item " + e); } finally { if (session != null) session.close(); } return findItem; }