Pin It

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"



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot