aspose file tools*
The moose likes Object Relational Mapping and the fly likes best way to implement java object identity and equality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "best way to implement java object identity and equality" Watch "best way to implement java object identity and equality" New topic
Author

best way to implement java object identity and equality

Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Which is the best way to implement java object identity and equality in hibernate and why?


thanks
Harvinder
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What is this, a job interview question? Or an assignment? We have reasons why we don't like answering these out right. So, what do you think?

Up front I will tell you that Hibernate does this for you so you have to think of why you would want change this behaviour and you will probably have your answer. Also what I would suggest is you think of the data you are mapping; it will already have identity in a different context. Does it make sense to change that?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
that's the response what i thought my subject would attract .... a possible interview question. I am sorry for that as i typed the question in a hurry.
Well honestly it is not. I was going through this official hibernate Tutorial which said

There is one caveat: Never use the database identifier to implement equality, use a business key, a combination of unique, usually immutable, attributes. The database identifier will change if a transient object is made persistent.


Generally, i use a tool that reverse engineers from a database table to create corresponding hibernate classes. That tool simply uses the table primary key and maps it to the <id> element.
Does database identifier mean "primary key"?
If yes then how does the database identifier change if i am not modifying it?
What am i missing here?

I know it might be a silly question but i would appreciate any help.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ah detached objects! If you add detached objects into the mix object identity and equality gets a bit muddy - as you will see here. Worth a read.
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
thanks a lot for the link
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
I think the issue is more related to transient objects. A transient object will have a null or 0 for its identifier. A persistent or detached object will have the identifier set. If you are comparing a transient object to a persistent/detached object and are using the primary key (database identifier) in the equals method, then they will not be equal, even though they have the same data.


Author: JBoss in Action, javidjamae.com, @javidjamae on Twitter
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: best way to implement java object identity and equality