0

I am facing 2 issues with hibernate load() and get()

  1. 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.

  1. 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.

Siva
  • 3,297
  • 7
  • 29
  • 35

2 Answers2

2

Hibernate Session.get(..) returns you the persistent entity of given class . Refer API Hibernate Session

So you need to refer this returned persistence instance to a Student class variable so that you can get your data. So your code needs the below modification:-

Student s1= null;
System.out.println("Student is calling with get()");        
s1 = (Student)session.get(Student.class,new Integer(107));    
System.out.println("Student called with get()");
System.out.println("Printing Student Name___"+s1.getMarks()); 

Regarding your second doubt you are passing a non persistent new object s2 to your load method and since there is no object in your persistent context so it is querying database to make your s2 update with the persistence state.

Doing the below instead would not immediately issue a database read:-

Student s2 = session.load(Student.class, new Integer(107));
Manjunath
  • 1,685
  • 9
  • 9
2

The output of your first example doesn't match the example code, but I can see the following problems:

You are calling session.get(), which will return the object, but you ignore the result. Try this:

System.out.println("Student is calling with get()");        
Student s1 = (Student) session.get(Student.class,new Integer(107));    
System.out.println("Student called with get()");
System.out.println("Printing Student Name___"+s1.getMarks());  

Similar with the load() example. The first parameter should be the class of the object you want to read. The call will return the object.

In both cases there is no need to create a new Student(), Hibernate will do that for you.

For more information on get() and load() see Hibernate: Difference between session.get and session.load

Community
  • 1
  • 1
Thomas Stets
  • 3,015
  • 4
  • 17
  • 29