-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Why equals method doens't work?
PostPosted: Fri Feb 11, 2005 12:34 pm 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
This is the test code:

Course course = courseService.load(new Long(1));
Quiz quiz = courseService.loadQuizById(new Long(8));
Quiz q = (Quiz)course.getQuizs().iterator().next();
if(quiz.equals(q)) {
System.out.println("TRUEEEEEEEEE");
} else {
System.out.println("FALSEEEE");
}

I stored only a course object and only a quiz object in the db.
Anyway I assure you that "quiz" and "q" are equals.

I won't give the xml because i don't think the problem can be there...these are very simple associations (course has many quiz, and a quiz belong to a course).

I overrided equals and hashCode methods:

public boolean equals(Object obj) {
if (obj == null) {
return false;
}
Quiz o = (Quiz) obj;
return title.equals(o.title) && course.equals(o.course);
}

public int hashCode() {
return title.hashCode();
}

So why they are not equals in that code?
Could be because of the proxy?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 12, 2005 6:55 am 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
If courseService.load has its own session and courseService.loadQuizById also has its own session then q and quiz can be diffrent objects so their courses can be diffrent to.
Try to make equals between object from one session (maby better transaction) or overwrite course.equals and use it as a base class for proxy.

_________________
Lmichasz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 12, 2005 7:51 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
Lmichasz wrote:
If courseService.load has its own session and courseService.loadQuizById also has its own session then q and quiz can be diffrent objects so their courses can be diffrent to.
Try to make equals between object from one session (maby better transaction) or overwrite course.equals and use it as a base class for proxy.


They have the same memory reference! and then title is the same, course is the same (course has a method equals that check its name).

If i added the line
if(this == o) return true;

everything is ok, but i don't understand this behavior...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 12, 2005 8:10 am 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
Try to use getters instead of direct properties in your equals method.

_________________
Lmichasz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.