Hibernate
Hibernate
follows.
By saving that object like above.
By loading object from database.
Key Points
Hibernate Object States – If an object created to class, then that is in transient state.
Hibernate Object States – If an object represent one row in database then that is persistent state.
Hibernate Object States – Object will be deleted permanently from the database if an object
moved from persistent state to transient state.
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.
Example
For Example while using the A.java class, It contains an Object like
// 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();
Class object of a class contains Metadata and Object of a class contains data.
Description
When reading an Object from Database using load(), Hibernate immediately does not call the
Database.
For Example, while reading the Id 414 details in Database using load(), it contains a POJO Class as
IToolsInfo.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.
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.
Example
Object object=session.get("IToolsInfo.class", 414);
While reading an Object from a Database and if it doesn’t exist in database, then the result will
be null.
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.
package com.abc;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
t.commit();//transaction is commited
session.close();
}
}