package edu.javacourse.hibernate;

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

import java.util.List;

/**
 * Простой пример работы со связанными таблицами
 *
 * @author ASaburov
 */
public class HibernateSimple {

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

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session s = sessionFactory.getCurrentSession();
        s.beginTransaction();

        Criteria criteria = s.createCriteria(City.class);
        List<City> cityList = criteria.list();

        for (City city : cityList) {
            log.debug("city id: {}", city.getCityId());
            log.debug("city name: {}", city.getCityName());
            log.debug("city region id: {}", city.getRegion().getRegionId());
            log.debug("city region name: {}", city.getRegion().getRegionName());
            log.debug("");
        }

        log.debug("====================================================");

        List<Region> regionList = s.createQuery("from Region").list();

        for (Region r : regionList) {
            log.debug("Region name: {}", r);
            for (City c : r.getCityList()) {
                log.debug("     City name: {}", c);
            }
        }

        Region spb = new Region("SPb");
        s.save(spb);

        City gatchina = new City();
        gatchina.setCityName("Gatchina");
        gatchina.setRegion(spb);
        s.save(gatchina);

        City pushkin = new City();
        pushkin.setCityName("Pushkin");
        pushkin.setRegion(spb);
        s.save(pushkin);

        s.save(spb);

        s.getTransaction().commit();
    }

}

@@ -130,68 +130,8 @@
 ry;%0A
-import org.hibernate.boot.registry.StandardServiceRegistry;%0A
 impo
@@ -229,24 +229,70 @@
 figuration;%0A
+import org.hibernate.service.ServiceRegistry;%0A
 import org.s
@@ -569,21 +569,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -583,31 +583,121 @@
 tic 
-void main(String%5B%5D args
+SessionFactory sessionFactory;%0A    private static ServiceRegistry serviceRegistry;%0A%0A    private static void init(
 ) %7B%0A
@@ -821,32 +821,8 @@
     
-StandardServiceRegistry 
 serv
@@ -934,31 +934,16 @@
         
-SessionFactory 
 sessionF
@@ -1002,16 +1002,192 @@
 gistry);
+%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      
@@ -2466,16 +2466,36 @@
 ommit();
+%0A%0A        destroy();
 %0A    %7D%0A%0A

Fix Hibernate Init and Destroy