Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
[b]Hibernate version:[/b] 3.0.5
Ok, here is a strange situation:
I have a User object which contains a set of Tasks. This is all occuring within Tomcat. The User is stored in Tomcat's session.
In one of my JSP tags, I get the User object from the HttpSession. Then I re-attach it by creating a Hibernate session and use session.update(user). Then I get the tasks set:
Set tasks = user.getTasks();
Before doing that, I load the task in by task ID number:
Task task = session.get(Task.class, taskIdNumber);
and I want to see if the task is one of the user's tasks. This is a security check; I want users to be able to look at their own tasks only.
So I use:
if(! tasks.contains(task)) { // access denied!
But it isn't working when it should. I have narrowed the problem down some.
First, the Hibernate session guarantees that objects which are persistent and attached will have:
a == b
if a and b refer to the same row in the DB. This is the case. In fact I can do an iterator through the tasks set and see that the exact same object (object identity, a == b) is within the set.
And yet contains() still returns false!
Can anyone say why this is? It seems like it's a serious violation of the Set contract. I'm totally baffled.
One thing I have done is to write my own equals() and hashCode() methods for Task. They do work correctly.
Any thoughts on this mysterious problem would be appreciated. Thanks.