Hi Gyanom,
Thanks for your comment, it is appreciated.
Yes, it is correct that without persisting the thing work fine.
But when the entity is retrieved by Hibernate, the this keyword acts in a funky way.
To be more precise, the linkCategory object retrieved by Hibernate is correctly set up.
The following code bit sitting in the test case method displays fine the line "The first if is true" in the console.
Code:
for (Link thelink : linkCategory.getLinks()) {
if (thelink.getLinkCategory() == linkCategory) {
logger.debug("========================>> The first if is true");
linkCategory.removeLink(thelink, linkCategory);
}
}
Now, note the redundant usage (for demo purposes only) of the linkCategory object in the call to removeLink above, once as the reference and once as a method argument.
Code:
linkCategory.removeLink(thelink, linkCategory);
I modified the method to accept this redundant parameter, as in:
Code:
public void removeLink(Link link, LinkCategory linkCategory) {
logger.debug("========================>> About to remove a link");
logger.debug("========================>> link.getLinkCategory: " + link.getLinkCategory() + " this: " + this);
if (link.getLinkCategory() == linkCategory) {
logger.debug("========================>> The second if is true");
}
if (link.getLinkCategory() == this) {
logger.debug("========================>> Removing a link");
link.setLinkCategory(null);
this.links.remove(link);
logger.debug("========================>> Removed a link");
}
}
The method call displays fine the text "The second if is true" in the console.
This means that the linkCategory object passed in the method parameter is correctly set up.
But none of the following texts "Removing a link" nor "Removed a link" are displayed.
This means that the this object is not correctly set up.
But the linkCategory object and the this object are one and the same, or they should be.
How to explain that the same linkCategory object used redundantly in the method call has different behaviors if used as a parameter or used as the this object ?
Here is the test case in full:
Code:
@Test
public void testCollection() {
link0 = new Link();
link0.setName("Thalasoft");
link0.setImage("image0.png");
link0.setListOrder(3);
linkCategory0.addLink(link0);
link2 = new Link();
link2.setName("Google");
link2.setImage("image2.gif");
link2.setListOrder(1);
linkCategory0.addLink(link2);
link1 = new Link();
link1.setName("Libe");
link1.setImage("image1.jpg");
link1.setListOrder(2);
linkCategory0.addLink(link1);
linkCategoryDao.flush();
linkCategoryDao.clear();
LinkCategory linkCategory = linkCategoryDao.findWithId(linkCategory0.getId(), false);
assertEquals(3, linkCategory.getLinks().size());
Iterator<Link> results = linkCategory.getLinks().iterator();
Link link = (Link) results.next();
assertEquals(link2.getName(), link.getName());
link = (Link) results.next();
assertEquals(link1.getName(), link.getName());
link = (Link) results.next();
assertEquals(link0.getName(), link.getName());
for (Link thelink : linkCategory.getLinks()) {
if (thelink.getLinkCategory() == linkCategory) {
logger.debug("========================>> The first if is true");
linkCategory.removeLink(thelink, linkCategory);
}
}
linkCategoryDao.flush();
linkCategoryDao.clear();
linkCategory = linkCategoryDao.findWithId(linkCategory0.getId(), false);
assertEquals(2, linkCategory.getLinks().size());
assertEquals(link2.getName(), linkCategory.getLinks().iterator().next().getName());
assertEquals(link0.getName(), linkCategory.getLinks().iterator().next().getName());
}