• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equal( ) and hashCode( ) Examples

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anybody have some examples on the use of equal() and hashCode() methods in Hibernate? Where are they usually used?

Sorry if this question sounds naive. I am new to Hibernate.

Thanks.

Lilly
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This requirement applies only to classes that you will put into <set> collections (maybe other collections, too; I've only used <set> so far). The reason is that sets use equals() to enforce non-duplicates, and HashSet uses hashCode() as a first-cut test for equality. I guess technically hashCode() isn't necessary then since equals() will always be used in the end, but providing a meaningful hashCode() will improve performance for very large sets or objects that take a long time to compare using equals().

Let's start with equals(). You need to define what equality means for each of your objects. Take a simple Employee example.Now, when are two Employees equal? If their first and last names match, they're probably the same employee, but that's not guaranteed. The same is true of Social Security Number, but they are designed to be not repeat for a long time (if I remember correctly; clearly there are more than 999,999,999 people so SSN can't always be unique), and most companies use this as their unique identifier. Just as important, an Employee's SSN will not change while the application is running.

All this together makes SSN a pretty good choice for equality().For hashCode(), I recommend reading the JavaDocs. It describes what rules the method must follow, especially with regard to the equals() method. The short of it is, you typically use the same information to calculate the hash code value as you used to determine equality.One important thing is that you do not change the Employee fields that are used for equality and hashing after puttnig them into a Set. The reason is that the Set will not be told that the hash code value has changed. If you do need to change an Employee's SSN, you must remove them from all Sets that contain them, change the value, then re-add them to the Sets.

Here's a simple program to demonstrate what happens if you don't follow the above rule.Note this is for JDK 1.5. Just remove the <TestHashSet> suffixes from the HashSet declaration and instantiation. And here's the output.Notice that the Set allowed a duplicate TestHashSet (Bob) to be added since the first Bob is still stoerd under the hash code 127.

Let me know if you have any questions with this. While the latter part may seem complicated, under typical usage (read object containing Set, remove some children, add some children, save object) you won't have to worry about it. Even if you load the object, change some fields used for the hash code value, and save the object you should be okay since you aren't adding new children at the same time (though you use a second-level cache you may run into problems).
[ January 29, 2005: Message edited by: David Harkness ]
 
Lilly Wiesie
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave,

Wow! That's pretty thorough. Thanks!

Lilly
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very welcome. And welcome to JavaRanch!

Hibernate's a great tool, but I know many people struggle with it in the beginning (I still am occassionally), and some of the concepts are a bit esoteric. Good luck!
 
Lilly Wiesie
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, looks like the learning curve is a bit steep (at least to me). But it's a pretty cool tool.

I really enjoy the peaceful environment in JavaRanch. It's a good place.

Lilly
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if you want to pick up two new tools at once, but I've found that the Spring Framework makes using Hibernate vastly more simple. It provides many abstraction classes (for example a more sane unchecked exception hierarchy and thread-bound sessions and transactions) and declarative transactions (a la CMT in EJBs).
 
Lilly Wiesie
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, David. I'll take a look of Spring framework.

I am actually working on a web service project. The reason I didn't look into details of Spring framework is that I didn't see any support of web service from Spring yet (or I colud be worng - hope I am wrong!)

Lilly
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lilly WS:
The reason I didn't look into details of Spring framework is that I didn't see any support of web service from Spring yet (or I colud be worng - hope I am wrong!)

Neither does Hibernate, but you still chose to use it! My point is that Spring provides a lot of other value, just like Hibernate. You'd create POJO business objects that perform the work (business logic) and your web services would be a thin facade on top.

I joke all in good fun; I hope that comes through. In any case, this is as close as I see Spring comes to supporting web services (a specialized case of remoting). It depends on the protocol you're using to serialize your messages. Spring supports Hessian, Burlap and its own format. JAX-RPC is listed as TODO.

I haven't done any work with web services (just read a little), so I can't help much beyond that. Good luck!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you are right. It's not a bad thing using the Spring framework in teh web service project. I've been hoping they would add the web service support in before I started the project. Evidentaily they are behind my schedule ).

Lilly
 
reply
    Bookmark Topic Watch Topic
  • New Topic