public Boolean addOrUpdateMedia(Session session, Media media) { try { // Need to save the loans before saving the media Map<Integer, Loan> tabEx = media.getTabExemplaries(); for (Iterator<Loan> i = tabEx.values().iterator(); i.hasNext(); ) session.saveOrUpdate(i.next()); // Persist the media session.saveOrUpdate(media); } catch (HibernateException pe) { System.err.println("Problème dans la sauvegarde "); pe.printStackTrace(); return false; } return true; }
@SuppressWarnings("unchecked") public Boolean delMedia(Session session, Media media) { try { // Need to delete the loans before deleting the media Map<Integer, Loan> tabEx = media.getTabExemplaries(); for (Iterator<Loan> i = tabEx.values().iterator(); i.hasNext(); ) session.delete(i.next()); // Also, all the bookings of the media need to be deleted List<Booking> bookings = (List<Booking>) session.createQuery("from Booking where media=:m").setParameter("m", media).list(); for (Booking b : bookings) session.delete(b); session.delete(media); } catch (HibernateException pe) { System.err.println("Problème dans la suppression "); pe.printStackTrace(); return false; } return true; }