remove not removing [message #389736] |
Thu, 25 June 2009 06:06  |
Eclipse User |
|
|
|
I have a list of Agent2User entities as the N side of a 1-N relation that also updates the 1 side by calling the setter. This could cause a loop, but the contains-remove setup below breaks that: if the entity is still in the list, remove it, then call the setter, which will then call the remove again, but the entity will not be in the list anymore and thus the loop breaks.
The code is below and there 5 debug lines mixed in (the actual method is only 3 lines).
public void removeAgent2Users(nl.knowledgeplaza.profiler.engine.model.Ag ent2User value)
{
System.out.println("agent2user list size = " + this.agent2Users.size());
for (Agent2User a : this.agent2Users) System.out.println(">>" + a + " / " + value + " / " + a.equals(value) );
if (value != null && this.agent2Users.contains(value))
{
boolean lWasRemoved = this.agent2Users.remove(value);
System.out.println("WasRemoved = " + lWasRemoved);
System.out.println("agent2user list size after remove = " + this.agent2Users.size());
for (Agent2User a : this.agent2Users) System.out.println(">>" + a + " / " + value + " / " + a.equals(value) );
value.setAgent( (Agent)null );
}
}
Output:
agent2user list size = 1
>>nl.knowledgeplaza.profiler.engine.model.Agent2User@5366ef, Id=1001 / nl.knowledgeplaza.profiler.engine.model.Agent2User@5366ef, Id=1001 / true
WasRemoved = true
agent2user list size after remove = 1
>>nl.knowledgeplaza.profiler.engine.model.Agent2User@5366ef, Id=1001 / nl.knowledgeplaza.profiler.engine.model.Agent2User@5366ef, Id=1001 / true
Explanation:
1. The method enters and the list reports a size of 1, the objects appear to be equal.
2. The if condition passes, so contains must have returned true.
3. The remove is executed and WasRemoved is true.
4. Then it turns out the list is NOT of size zero and the entity is still present!!!
How can this be?
I tried to reproduce this in a standalone example, but I cannot. This only happens if the entity is returned via a SOAP interface. That doesn't solve my problem though.
Tom
|
|
|
|
|
Re: remove not removing [message #389744 is a reply to message #389743] |
Thu, 25 June 2009 10:15  |
Eclipse User |
|
|
|
> My guess is you have two copies of the source object. Try printing the
> source object as well.
>
> i.e.
> Agent->Agent2User->Agent'
>
> Seems like you corrupted your object with multiple copies, you may be
> missing a merge somewhere.
Aside from that my problem is gone now; the debug output printed the memory address of the object (@5366ef) and both were identical. The contains and remove were done on the same list in the same method. IF it passed "contains" then it should have been removed. Right?
But alas, it is gone.
Tom
|
|
|
Powered by
FUDForum. Page generated in 0.09290 seconds