This is working code You can try it.
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
// process your business logic.
session.getTransaction().commit();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…