Newbie to Hibernate.
I am just trying to understand session.get()
method "avoiding a database hit if the object is already cached"
List<Employee> employees1 = session.createCriteria(Employee.class).list();
Main main = new Main();
try {
synchronized (main) {
main.wait(10000);
}
} catch (InterruptedException e) {e.printStackTrace();}
Employee employees2 = (Employee)session.get(Employee.class, new Long(1));
System.out.println(employees2.getFirstname() + " , "+ employees2.getLastname() );
In the first line i got a record with firstname as 'Kevin' lastname as 'Papad'
While it was waiting i went to database and changed firstname to 'steve'
session.get()
is not hitting database and getting me values firstname as 'Kevin' lastname as 'Papad' which is WRONG.
I kept wait because anybody can change values in database meanwhile (between 1st and 3rd steps above), in that case how can i get correct values in 3rd step? i.e., firstname to 'steve'
What changes do i need to make to my code?
Please correct me if my understanding/approach is wrong.