java - Can't quite understand how to delete fields properly with Hibernate -
i have many-to-many relation in database (entities participant , event)
//part of participant model @manytomany(fetch = fetchtype.lazy , cascade = { cascadetype.persist, cascadetype.merge}) @jointable(name = "participant_event", joincolumns = {@joincolumn(name = "participant_id")}, inversejoincolumns = {@joincolumn(name = "event_id")}) //part of event model @manytomany(fetch = fetchtype.lazy, mappedby = "events", cascade = cascadetype.all)
right @ moment, deleting event causes deleting participants visit event. removing cascadetype.all
in event model causes no result after deleting event (by deleting mean .remove
). proper way delete event , keep participants?
code here
in many-to-many association, cascadetype.remove
not desirable, since deletes actual entities (not associated link table entries).
the proper way delete associations dissociate entities:
public void removeevent(event event) { events.remove(event); event.setparticipant(null); }
to delete event
, you'd have remove participants
. have this:
event deletableevent = ...; for(iterator<participant> participantiterator = event.getparticipnts(); participantiterator.hasnext();) { participant participant = participantiterator.next(); participant.getevents().remove(deletableevent); participantiterator.remove(); } entitymanager.flush(); entitymanager.remove(deletableevent);
Comments
Post a Comment