Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to create Hibernate's SessionFactory and Session Using Hibernate's Configuration file ?.

Program to demonstrate how to create Hibernate's SessionFactory and Session Using Hibernate's Configuration file in Java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTest {

   public static void main(String[] args) {
  
        // SessionFactory gives us a factory of sessions
    // Usually SessionFactory is been configured by the 
 // configuration file named as hibernate.cfg.xml
 // buildSessionFactory() builds the sessionFactory for us
 SessionFactory sessionFactory = new Configuration().
   configure().buildSessionFactory();
  
 // Session is created by calling openSession()
 // method on SessionFactory object
 Session session = sessionFactory.openSession();
 session.beginTransaction();

 // Our Transaction Code goes here 
 // i.e save, load , merge and remove
  
 session.getTransaction().commit();
 session.close();

   }
}




Hibernate Configuration file as : "hibernate.cfg.xml"



 
© 2021 Learn Java by Examples Template by Hubberspot