-
1. Re: Many-toMany, Mysql trying to remove record
taggat Aug 26, 2005 10:46 AM (in response to taggat)I just removed the constriants from the database, to find that the link tables records still exist after the deletion.
-
2. Re: Many-toMany, Mysql trying to remove record
taggat Aug 26, 2005 1:13 PM (in response to taggat)I forgot to put in that all the remove code is doing is this:
Group group = em.find(Group.class, id);
em.remove(group);
I even tried:
Group group = em.find(Group.class, id);
List users = group.getUsers();
List roles = group.getRoles();
for (User u:users) {
u.getRoles().remove(group);
}
group.setUsers(new ArrayList());
for (Role r:roles) {
r.getUsers().remove(group);
}
group.setRoles(new ArrayList());
em.flush();
em.remove(group);
but it came up with an error about trying to persist a record that has been deleted. -
3. Re: Many-toMany, Mysql trying to remove record
taggat Aug 26, 2005 1:15 PM (in response to taggat)Try that again
I forgot to put in that all the remove code is doing is this:Group group = em.find(Group.class, id); em.remove(group);
I even tried:Group group = em.find(Group.class, id); List users = group.getUsers(); List roles = group.getRoles(); for (User u:users) { u.getRoles().remove(group); } group.setUsers(new ArrayList()); for (Role r:roles) { r.getUsers().remove(group); } group.setRoles(new ArrayList()); em.flush(); em.remove(group);
but it came up with an error about trying to persist a record that has been deleted. -
4. Re: Many-toMany, Mysql trying to remove record
epbernard Sep 1, 2005 6:39 AM (in response to taggat)the mappedBy side is not responsible for managing the fk updates. you have to remove the groups from the suer side, or move the mapped by to the other side
-
5. Re: Many-toMany, Mysql trying to remove record
taggat Sep 1, 2005 6:50 AM (in response to taggat)ok, but i have the problem that i want to be able to call the same functionality from either side.
I have got around the problem by putting in 2 calls to the session bean, 1 that removes all the relation ships, then a second that deletes the entry. This works ok, but is not the best way to do it
The reason for doing it in 2 calls is because when doing it in a single call hibernate attempts to remove the record before the relationships are removed. -
6. Re: Many-toMany, Mysql trying to remove record
epbernard Sep 1, 2005 12:21 PM (in response to taggat)add a method on your entities (addEleemntToCollection() and removeElementToCollection() that will keep both side in sync.