@Override public void saveStakeholder( String name, String type, String email, String website, List<Conference> conferences) { if (conferences == null) conferences = new ArrayList<>(); Stakeholder s = new Stakeholder(); List<Key> keys = new ArrayList<>(); try { for (Conference c : conferences) { keys.add(c.getId()); } s = new Stakeholder(name, type, email, website, keys); em.persist(s); } finally { em.close(); } try { em = EMF.get().createEntityManager(); for (Key k : keys) { Conference c = em.find(Conference.class, k); c.addStakeholder(s.getId()); } } finally { em.close(); } }
@Override public void saveConference( String title, Date start, Date end, String room, String website, List<Stakeholder> stakeholders) { if (stakeholders == null) stakeholders = new ArrayList<>(); Conference c = new Conference(); List<Key> keys = new ArrayList<>(); try { for (Stakeholder s : stakeholders) { keys.add(s.getId()); } c = new Conference(title, start, end, room, website, keys); em.persist(c); } finally { em.close(); } try { em = EMF.get().createEntityManager(); for (Key k : keys) { Stakeholder s = em.find(Stakeholder.class, k); s.addConference(c.getId()); } } finally { em.close(); } }
@Override public void deleteConference(long conferenceId) { try { Conference c = em.find(Conference.class, conferenceId); em.getTransaction().begin(); // first remove related relation ... for (Key Skey : c.getStakeholders()) { Stakeholder s = em.find(Stakeholder.class, Skey.getId()); s.getConferences().remove(c.getId()); em.persist(s); } // then remove the entity em.remove(c); em.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } finally { em.close(); } }