• 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

best way to implement java object identity and equality

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is the best way to implement java object identity and equality in hibernate and why?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for the link
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic