I am facing 2 issues with hibernate load() and get()
get() always returning NULL, even though the object is present in the database.
Student s1=new Student(); System.out.println("Student is calling with get()"); session.get(Student.class,new Integer(107)); System.out.println("Student called with get()"); System.out.println("Printing Student Name___"+s1.getMarks()); System.out.println("--------------------------------"); Student s2=new Student(); System.out.println("Student is calling with load()"); session.load(s2,new Integer(107)); System.out.println("Student called with load()"); System.out.println("Printing Student Name___"+s2.getStdName());
Output:
Student is calling with load()
Hibernate: select student0_.stdtId as stdtId1_0_, student0_.stdName as stdName1_0_, student0_.marks as marks1_0_ from Student student0_ where student0_.stdtId=?
Student called with load()
Printing Student Name___0.0
--------------------------------
Student is calling with get()
Student called with get()
Printing Student Name___null
But if i called the same object with load() alone, its working fine and giving the output.
As it said, load() will hit the database only when ever we call its properties, but here its hitting the database immediately after calling load() method, check this source code..
Student s2=new Student(); System.out.println("Student is calling with get()"); session.load(s2,new Integer(107)); System.out.println("Student called with get()"); System.out.println("Printing Student Name___"+s2.getStdName());
Output:
Student is calling with get()
Hibernate: select student0_.stdtId as stdtId1_0_, student0_.stdName as stdName1_0_, student0_.marks as marks1_0_ from Student student0_ where student0_.stdtId=?
Student called with get()
Printing Student Name___Jaswanth
Can you please suggest where am doing the mistake ? thank you.