This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
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 ?
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.