package edu.javacourse.hibernate;

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 HibernateOneToOne {

    private static final Logger log = LoggerFactory.getLogger(HibernateOneToOne.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.openSession();
        s.beginTransaction();
        List<Region> regionList = s.createQuery("from Region").list();

        for (Region r : regionList) {
            System.out.println("Region name:" + r);
            System.out.println("Gubernator name:" + r.getGubernator().getName());
        }

        Region spb = new Region("SPb");
        Gubernator gubernator = new Gubernator("Poltavchenko");

        spb.setGubernator(gubernator);
        gubernator.setRegion(spb);

        s.save(spb);

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

        for (Region r : regionList) {
            System.out.println("Region name:" + r);
            System.out.println("Gubernator name:" + r.getGubernator().getName());

        }

        s.getTransaction().commit();

        log.debug("Transaction committed");
    }

}

@@ -99,68 +99,8 @@
 ry;%0A
-import org.hibernate.boot.registry.StandardServiceRegistry;%0A
 impo
@@ -198,24 +198,70 @@
 figuration;%0A
+import org.hibernate.service.ServiceRegistry;%0A
 import org.s
@@ -542,21 +542,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -556,31 +556,121 @@
 tic 
-void main(String%5B%5D args
+SessionFactory sessionFactory;%0A    private static ServiceRegistry serviceRegistry;%0A%0A    private static void init(
 ) %7B%0A
@@ -794,32 +794,8 @@
     
-StandardServiceRegistry 
 serv
@@ -907,31 +907,16 @@
         
-SessionFactory 
 sessionF
@@ -975,16 +975,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      
@@ -2014,16 +2014,36 @@
 itted%22);
+%0A%0A        destroy();
 %0A    %7D%0A%0A

Fix Hibernate initialization