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-

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 .




Related Posts:

  • Open Session In View(OSIV) Once the session is closed you can not be able to access the data from detached object and if you do you will get a LazyInitializationException: Session has been closed. It means that your session and session transaction is… Read More
  • Hibernate Transaction Management Transaction is the process of performing multiple database operations as one unit with all nothing criteria. I:e when all the database operations in the unit are successful then transaction is successful and should be commit… Read More
  • Hibernate Architecture Configuration or AnnotationConfiguration : These are available in org.hibernate.cfg package. This is the first class that will be instantiated by hibernate application. You can use Configuration or AnnotationConfigurati… Read More
  • Hibernate Cache Cache is representation of database near to application. when you cache the read-mostly(which will be repeatedly used) data, you can reduce the number of round trip between your application server and database server. Which … Read More
  • Alternatives of Open Session In View(OSIV) Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These … Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic