• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Hibernte :Can we update based on Unique key instead of PKey

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a database where most of our Primary keys are IDs.

In hibernate we can easily use the saveOrUpdate(Object) method which would save or update the data. Now, we have IDs as primary keys. These IDs are genearted by Hibernate. So whenever we try to update an object(using saveOrUpdate(..) method) it will treat the object as a NEW object. Had we used some column as PKey than, I guess, we would NOT have faced this issue. But now, instead of updating an existing col, it is actually inserting a new . This is , as I mentioned above, due to the fact that Pkey is the generated id and NOT the unique key!

Thus, I wanted to know if there is any way to overcome this issue ! There is a 'Unique Key' within each table and we would rather like the AUTOMATIC saveOrUpdate to be based on this 'UNIQUE' key instead of the Pkey. Is that possible ?

Any suggestions / feedback / solution will be much appreciated. I am not sure if this is due to incorrect / poor designing of DB tables where we should have used the Unique Keys instead of IDs as Pkey...But in any case we now need to resolve this without changing any DB tables.

Thanks and regards,
Milan Doshi
 
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 the differernce between a Primary key and an ID (assuming your id is unique and unchanging)?


So whenever we try to update an object(using saveOrUpdate(..) method) it will treat the object as a NEW object


It will treat the object as an existing object is you loaded it through Hibernate.
[ December 03, 2008: Message edited by: Paul Sturrock ]
 
Doshi Milan
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for your reply,

I guess I have not really understood your questions or maybe I am missing something.

What is the differernce between a Primary key and an ID (assuming your id is unique and unchanging)?



We have various Generated Ids that act as Primary Key. It is <b>Different</b> from the the Composite Unique keys that we are using.

It will treat the object as an existing object is you loaded it through Hibernate.



As I mentioned earlier, the Unique key in our tables is NOT the primary key.My limited understanding states that Hibernate would have treated the object as an existing object , if and only if,the Pkey is the same. However in our case, as we genearte a new Pkey Hibernate is NOT able to LOAD based on UNIQUE keys.

Hence my original question : Can we have our saveOrUpdate to be based on the 'UNIQUE' key instead of the Pkey. Is that possible ?

Thanks,
Milan Doshi
 
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
I suppose the gist of my question is why use a unique composite identifier (presumably implemented as a unique index?) and a primary key? What is it you are trying to achieve - surely both a unique index and a primary key in a relational database serve the same purpose (at least when it comes to uniquely identifying a row)?


As I mentioned earlier, the Unique key in our tables is NOT the primary key


So it doesn't uniquely identify a row? In which case (whether using Hibernate or not) how do you safely identify a specific row?


However in our case, as we genearte a new Pkey Hibernate is NOT able to LOAD based on UNIQUE keys.


If you want to load anything in Hibernate and you are not using a primary key, the normal way to do this is via a query.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic