In case of JDBC we the developer are responsible for establishing and terminating the database connection but in case of Hibernate, it is only responsible for establishing and terminating database Connectivity , we only need to indicate by simply calling some pre-defined method by the hibernate session. For ex-
In hibernate
1 . Insert can be performed by calling save() method.
2. Select can be performed by calling load() method. etc
How to create session In hibernate
1. Creating the SessionFactory instance.
After getting the SessionFactory which holds all the information about the database by reading the hibernate Configuration file, need to call the openSession() of SessionFactory interface which an abstract method and returns the Session instance.
Initializing Session instance creating the database Connection and opening the session which involves the database activity like insert,delete, update etc.
In hibernate
1 . Insert can be performed by calling save() method.
2. Select can be performed by calling load() method. etc
How to create session In hibernate
1. Creating the SessionFactory instance.
SessionFactory factory=null; Configuration cfg=new Configuration(); cfg=cfg.Configure(); factory=cfg.buildSessionfactory();
2. After creating Sessionfactory call openSession() method which returns the Session instance. Like this
Session session = factory.openSession();
By calling openSession() a session will be created and a session is responsible for performing a database operation. For multiple database operation we must have that much Session instance.
Note: Session is not a thread -safe that means as long as it is open it is in risk of accessible by other thread also so it must be closed after operation is over.
How to close session?
It can be simply closed by calling close() with Session instance. Like this
session.close();
States of Session Instance.
There are three states of instance of Session which behaves according to the persistence object creation.
1. Transient: It is the initial state which represents that persistent class instance is not associated with any of the Session. That means it doesn't involve with any activity/operation with the database.
2. Persistent: It is the state of Session which represents that persistent class instance is associated with the Session and it has representation in the database and hence will be involved with the database activity/operation.
3. Detached: It is the final state of Session which represents that the Session is closed and hence persistent class instance get detached.
Note: After Session creation Transaction creation is mandatory because we need to commit every operation/activity we are performing within the session. Otherwise changes will not reflect in the database. And by default auto-commit is disable or de-active so we need to commit explicitly for that we need to create transaction for each database operation.
For details about Hibernate Transaction click here Hibernate Transaction .
Great Article
ReplyDeleteIEEE Final Year Projects for CSE IEEE Project Centers in Chennai