Program to demonstrate how to create Hibernate's SessionFactory and Session Using Hibernate's Configuration file in Java
Hibernate Configuration file as : "hibernate.cfg.xml"
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"
