0% found this document useful (0 votes)
2 views

Hibernate

Uploaded by

sriedorresume
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Hibernate

Uploaded by

sriedorresume
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

if one want to convert the object from Transient state to Persistent state one can do in 2 ways as

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.

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.

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;

public class StoreData {


public static void main(String[] args) {

//creating configuration object


Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the
configuration file

//creating seession factory object


SessionFactory factory=cfg.buildSessionFactory();

//creating session object


Session session=factory.openSession();

//creating transaction object


Transaction t=session.beginTransaction();

Employee e1=new Employee();


e1.setId(122);
e1.setFirstName("sonoorrr");
e1.setLastName("jaiswalrrrr");

session.persist(e1);//persisting the object

t.commit();//transaction is commited
session.close();

System.out.println("your data has been stored successfully, please check


in your data base.");

}
}

You might also like