Hibernate - SPLessons

Hibernate Object Reading

Home > Lesson > Chapter 13
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate Object Reading

Hibernate Object Reading

shape Description

Hibernate Object Reading is the one of the main interview question, To read an Object from Database, Hibernate Object Reading provides two methods in Session Interface, both will be called as session. get() and session.load(), the functionality of both methods are same that retrieving an object from the database. These Two methods contain two parameters like Class Object of class and Primary Key Column Id in the form of the Serializable object.

shape Syntax

Class object of class means whenever class is compiled, JVM will create a Class Object of class. Every class contains one Class Object of class and more number of Object of classes.

shape Example

For Example while using the A.java class, It contains an Object like [java] // It is an Example of Class Object of Class. Class class=A.class // This is an Example of Object of Class. A a1=new A(); A a2=new A(); [/java] Class object of a class contains Metadata and Object of a class contains data.

load()

shape Description

When reading an Object from Database using load(), Hibernate immediately does not call the Database.
  1. Hibernate creates a Proxy class by extending POJO Class.
  2. Hibernate creates an object of Proxy class and it will set Id to it.
  3. Returns the Proxy Object value into Java code. Proxy Object is a temporary object.
  4. When reading an Object is Non-Id property then internally Hibernate reads the data from Database and it will set the data to Proxy Object. Then Proxy Object converts to Real POJO Class Object.
  5. Again the same Object is read and the Process repeats. It is called Lazy Loading.
  6. While reading an Object from a Database and if the object doesn't exist on database, load() throws ObjectNotFound Exception.

shape Example

For Example, while reading the Id 414 details in Database using load(), it contains a POJO Class as IToolsInfo.Java. [java] Object object = session.load(IToolsInfo.class,414); IToolsInfo itoolsinfo=(IToolsInfo).object;//This itoolsinfo object is Proxy object. int x=itoolsinfo.getId();// This is also proxy object. String s=itoolsinfo.getName();// This itoolsinfo object is real object. [/java]

get()

shape Description

When get() is called then Hibernate immediately calls the Database and reads the data from Database to return the Real Object. It is called Early loading.
  1. Internally Hibernate uses the JDBC API.So, first Hibernate reads the data from Database and it store the data in ResultSet. Then copies the data in POJO class Object.
  2. When same object is read for second time, then Hibernate first checks the ResultSet Object whether this object exists or not.If exists, it copies the data from ResultSet Object to POJO class Object. If not exists, reads the data from Database and gives the result.

shape Example

[java] Object object=session.get("IToolsInfo.class", 414); [/java] While reading an Object from a Database and if it doesn't exist in database, then the result will be null.

Summary

shape Key Points

  • Hibernate Object Reading - In load() method Hibernate will create fake object.
  • Hibernate Object Reading - In get() object will be retrieved immediately  from the database.