In user model class I have the following:
public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ManyToMany(mappedBy = "attendees", cascade = CascadeType.ALL) @Cascade(org.hibernate.annotations.CascadeType.ALL) private Set timeslots = new HashSet();
And i would like to delete the timeslots..i've tried something but doesnt work, as follows:
public static boolean deleteUserTimeslot(EntityManager em, Timeslot ts) { EntityTransaction transaction = em.getTransaction(); try { ArrayList<User> attendeeList = (ArrayList<User>) ts.getAttendees(); List<User> attendeesToRemove = (List<User>) getAllUsers(em); transaction.begin(); for(User u: attendeesToRemove){ for(int i=0;i<attendeeList.size();i++){ if(attendeeList.get(i).getId()==u.getId()){ em.remove(u.getTimeslots()); break; } } } transaction.commit(); return true; } catch (PersistenceException ex) { //Rolling back data transactions if (transaction != null && transaction.isActive()) { transaction.rollback(); } logger.error("Error making database call for update timeslot status"); ex.printStackTrace(); } catch (Exception e) { logger.error(e.getMessage(), e); e.printStackTrace(); } return false; }