package database;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import foo.bar.entities.Event;

/**
 * 
 * http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/
 * Hibernate_User_Guide.html#bootstrap
 * 
 * http://stackoverflow.com/questions/25684785/how-to-read-database-
 * configuration-parameter-using-properties-file-in-hibernate
 * 
 * @author Fernando Felix do Nascimento Junior
 *
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory;

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void setSessionFactory(SessionFactory sessionFactory) {
		HibernateUtil.sessionFactory = sessionFactory;
	}

	/**
	 * Build a standard session factory for the application based on settings of
	 * the default Hibernate configuration.
	 * 
	 * @return A standard session factory
	 */
	public static SessionFactory buildSessionFactory() {
		if (sessionFactory == null) {
			StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
			MetadataSources sources = new MetadataSources(registry);
			sessionFactory = sources.buildMetadata().buildSessionFactory();
		}
		return sessionFactory;
	}

	public static Session getSession() {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (org.hibernate.HibernateException he) {
			session = sessionFactory.openSession();
		}
		return session;
	}

	/**
	 * Creates a Hibernate configuration based on a resource file wit Annotated
	 * classes registered dynamically.
	 * 
	 * @param resource
	 *            The resource name. If none given, Hibernate handles the
	 *            default configuration resource.
	 * @see HibernateUtil#registerAnnoteatedClass(Configuration)
	 * 
	 * @return A Hibernate configuration
	 */
	public static Configuration createConfiguration(String resource) {
		Configuration configuration = new Configuration();
		if (resource == null)
			configuration.configure();
		else
			configuration.configure(resource);
		registerAnnoteatedClass(configuration);
		return configuration;
	}

	/**
	 * Adds the entity model classes into a configuration file that will be used
	 * by Hibernate to map.
	 * 
	 * @param configuration
	 */
	public static void registerAnnoteatedClass(Configuration configuration) {
		configuration.addAnnotatedClass(Event.class);
	}

}

@@ -44,215 +44,15 @@
 sion
-;%0Aimport org.hibernate.SessionFactory;%0Aimport org.hibernate.boot.MetadataSources;%0Aimport org.hibernate.boot.registry.StandardServiceRegistry;%0Aimport org.hibernate.boot.registry.StandardServiceRegistryBuilder
+Factory
 ;%0Aim
@@ -977,495 +977,116 @@
 %0A%09%09%09
-StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();%0A%09%09%09MetadataSources sources = new MetadataSources(registry);%0A%09%09%09sessionFactory = sources.buildMetadata().buildSessionFactory();%0A%09%09%7D%0A%09%09return sessionFactory;%0A%09%7D%0A%0A%09public static Session getSession() %7B%0A%09%09Session session = null;%0A%09%09try %7B%0A%09%09%09session = sessionFactory.getCurrentSession();%0A%09%09%7D catch (org.hibernate.HibernateException he) %7B%0A%09%09%09session = sessionFactory.openSession();%0A%09%09%7D%0A%09%09return session
+sessionFactory = createConfiguration(%22hibernate.cfg.xml%22).buildSessionFactory();%0A%09%09%7D%0A%09%09return sessionFactory
 ;%0A%09%7D

Simplify HibernateUtil