I am going through lock method of hibernate. I did not get what we are trying to achieve through this method.
p1 = (Person)session. get(Person.class,1);// person name is scott here
// here peson name got update to henry by anothet thread
session. lock(person, LockMode.READ) line 3
// still name is henry too
I tried above code snippet but it did not read the person from database at line 3.
Documentation says that This may be used to perform a version check.
Not sure how does it check version and helps the developer in any scenario?
Similarly not sure what session.lock(person, LockMode.None) will achieve. Documentation says LockMode.NONE is used to to simply re-associate a transient instance with a session.
Not sure what does it mean by reassociating a transient instance with session. A brief scenario will be a great help?
The different lock-modes are meant for preventing the entity from being modified and read from multiple sources simultaneously, see the documentation entry about pessimistic locking for details. In my experience, these are rarely needed, as the database isolation level usually takes care of locking as needed:
As for the “reassociating a transient instance with the session” (I actually think they mean detached instance?), consider the following picture (Hibernate entity lifecycle):
This is the description from Hibernate community documentation: