package edu.javacourse.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Простой пример для конфигурации в виде XML
 *
 * @author ASaburov
 */
public class HibernateConfigXMLMappingAnnotations {

    private static final Logger log = LoggerFactory.getLogger(HibernateConfigXMLMappingAnnotations.class);

    public static void main(String[] args) {
        HibernateConfigXMLMappingAnnotations hs = new HibernateConfigXMLMappingAnnotations();

        SessionFactory sessionFactory = hs.getSessionFactory();
        Session s = sessionFactory.getCurrentSession();
        s.beginTransaction();

        s.save(new Region("Krasnoe Selo " + System.nanoTime()));

        List<Region> regionList = s.createQuery("from Region").list();
        for (Region r : regionList) {
            System.out.println(r);
        }

        s.getTransaction().commit();

        log.debug("Transaction committed");
    }

    private SessionFactory getSessionFactory() {

        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory(
                new StandardServiceRegistryBuilder()
                        .applySettings( configuration.getProperties() )
                        .build()
        );
        return sessionFactory;

    }
}

@@ -198,24 +198,70 @@
 figuration;%0A
+import org.hibernate.service.ServiceRegistry;%0A
 import org.s
@@ -327,60 +327,12 @@
 til.
-Date;%0Aimport java.util.List;%0Aimport java.util.Random
+List
 ;%0A%0A/
@@ -577,21 +577,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -591,129 +591,354 @@
 tic 
-void main(String%5B%5D args) %7B%0A        HibernateConfigXMLMappingAnnotations hs = new HibernateConfigXMLMappingAnnotations();%0A
+SessionFactory sessionFactory;%0A    private static ServiceRegistry serviceRegistry;%0A%0A    private static void init() %7B%0A        Configuration configuration = new Configuration();%0A        configuration.configure(%22hibernate.cfg.xml%22);%0A        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
 %0A   
@@ -934,33 +934,33 @@
 uild();%0A        
-S
+s
 essionFactory se
@@ -961,49 +961,239 @@
 ory 
-sessionFactory = hs.getSessionFactory
+= configuration.buildSessionFactory(serviceRegistry);%0A    %7D%0A%0A    private static void destroy() %7B%0A        StandardServiceRegistryBuilder.destroy(serviceRegistry);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        init
 ();%0A
+%0A
     
@@ -1582,411 +1582,27 @@
 %22);%0A
-    %7D%0A%0A    private SessionFactory getSessionFactory() %7B%0A%0A        Configuration configuration = new Configuration().configure();%0A        SessionFactory sessionFactory = configuration.buildSessionFactory(%0A                new StandardServiceRegistryBuilder()%0A                        .applySettings( configuration.getProperties() )%0A                        .build()%0A        );%0A        return sessionFactory;%0A
+%0A        destroy();
 %0A   

Fix Hibernate initialization