I have a table that uses a surrogate primary key. The natural key of the entity (table) is namespace + name. The web application accessing the table will offer the user a web form that allows the user to update any property of the entity. I am using Hibernate for ORM. When issuing a session.update(entity), the entity would need to have its ID (surrogate key) populated to allow Hibernate to identify the record in the database. I can think of 2 ways of doing this
(1) pass the ID (hidden) to the front-end and then back to the business layer upon update on the form
(2) pass only the natural keys and do a lookup using natural keys in the DAO layer to get the ID.
Note that the natural keys cannot be updated using the form. There is a separate form for altering the identity (namespace + name) of the object and concurrency isn’t an issue i.e. no one would be able to alter the identity while someone else holds a lock for update so looking up using the natural key would still yield the same record that was used while requesting the update.
Questions
(1)Are there any approaches besides the ones I have mentioned?
(2)How is this typically accomplished in web applications?
I tried searching but couldn’t find much information on this; probably not searching with the correct choice of words.
Both approaches are valid and I don’t even know what to say more. It’s up to you how to implement this, though if you’re using surrogate key and 2nd level cache is activated, then entity might be taken from the cache directly which will increase the performance if those operations are often used. Searching by natural key will bypass the cache.