I have a question regarding the way Hibernate works with dirty checking.
Assume I have 2 users A and B performing the same action: retrieving a list of all records in a table, say APPLICATION table. They will get 2 lists which contain the same data.
Now user B changed some data and committed it to database. After that, what will happen if user A also change some data and commit it to database?
I know that Hibernate will do dirty checking, but does it only check with user A's session (which doesn't represent current data in database) or does it check with data in the database? Does it know that user B has changed some data since the time data is retrieved for user A?
(Afaik, session is just a cache, so it which doesn't represent current data in the database)
I'm asking this because I want to force user A to refresh the page to see latest data before changing anything.
Robert Roberto wrote:So it looks like I need a "version" column in every table that I want to ensure this condition?
Robert Roberto wrote:And Hibernate only checks with session and not the database data?
From your example: user A and B get a list of products and both want to update product 5. When user B updates product 5, the value in the version column is updated with value 2 (was 1). user A still sees product 5 with version 1 (and doesn't know about the update of user B). When user A hits the save button of product 5, hibernate tries to execute an update query like UPDATE product SET name = 'New name' WHERE id = 5 AND version = 1. But this query will fail, because this row doesn't exist anymore (only the row with id=5 and version=2 exists). So an exception will be thrown and you can handle it appropriately (inform user A the update failed and show the most recent version of the product).
I used it in one of my previous projects I worked on and it works pretty well