package database;

import java.util.Properties;

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 final SessionFactory sessionFactory;
	//
	// static {
	// try {
	// sessionFactory =
	// createSessionFactory(createConfiguration("hibernate.cfg.xml"));
	// } catch (Throwable ex) {
	// System.err.println("Initial SessionFactory creation failed." + ex);
	// throw new ExceptionInInitializerError(ex);
	// }
	// }
	//
	// public static SessionFactory getSessionFactory() {
	// return sessionFactory;
	// }

	public static SessionFactory createSessionFactory(String resourceName, Properties properties) {
		Configuration configuration = createConfiguration(resourceName, properties);
		StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
				.applySettings(configuration.getProperties()).build();
		MetadataSources sources = new MetadataSources(registry);
		return sources.buildMetadata().buildSessionFactory();
	}

	public static SessionFactory createSessionFactory(Configuration configuration) {
		StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
				.applySettings(configuration.getProperties()).build();
		MetadataSources sources = new MetadataSources(registry);
		return sources.buildMetadata().buildSessionFactory();
	}

	public static void registerAnnoteatedClass(Configuration configuration) {
		configuration.addAnnotatedClass(Event.class);
	}

	public static Configuration createConfiguration(String resourceName) {
		return createConfiguration(resourceName, null);
	}

	/**
	 * Creates a Hibernate configuration based on a resource file. A properties
	 * object can be passed optionally as argument to add or overwrite resource
	 * settings.
	 * 
	 * @param resource
	 *            The resource name. If not given, Hibernate handles the default
	 *            configuration resource.
	 * @param properties
	 *            Optional properties
	 * @return A Hibernate configuration
	 */
	public static Configuration createConfiguration(String resource, Properties properties) {
		Configuration configuration = new Configuration();
		if (resource == null)
			configuration.configure();
		else
			configuration.configure(resource);
		if (properties != null)
			configuration.addProperties(properties);
		registerAnnoteatedClass(configuration);
		return configuration;
	}

}

@@ -16,38 +16,8 @@
 e;%0A%0A
-import java.util.Properties;%0A%0A
 impo
@@ -1058,434 +1058,195 @@
 %7D%0A%0A%09
-public static SessionFactory createS
+/**%0A%09 * Creates a standard s
 ession
-F
+ f
 actory
-(String resourceName, Properties properties) %7B%0A%09%09Configuration configuration = createConfiguration(resourceName, properties);%0A%09%09StandardServiceRegistry registry = new StandardServiceRegistryBuilder()%0A%09%09%09%09.applySettings(configuration.getProperties()).build();%0A%09%09MetadataSources sources = new MetadataSources(registry);%0A%09%09return sources.buildMe
+ based on settings of a configuration.%0A%09 * %0A%09 * @param configuration%0A%09 *            The configuration%0A%09 * @return A s
 ta
+n
 da
-ta().buildS
+rd s
 ession
-F
+ f
 actory
-();%0A%09%7D%0A
+%0A%09 */
 %0A%09pu
@@ -1581,431 +1581,123 @@
 %7D%0A%0A%09
-public static void registerAnnoteatedClass(Configuration configuration) %7B%0A%09%09configuration.addAnnotatedClass(Event.class);%0A%09%7D%0A%0A%09public static Configuration createConfiguration(String resourceName) %7B%0A%09%09return createConfiguration(resourceName, null);%0A%09%7D%0A%0A%09/**%0A%09 * Creates a Hibernate configuration based on a resource file. A properties%0A%09 * object can be passed optionally as argument to add or overwri
+/**%0A%09 * Creates a Hibernate configuration based on a resource file wit Annotated%0A%09 * classes regis
 te
- 
 re
-source%0A%09 * settings
+d dynamically
 .%0A%09 
@@ -1758,17 +1758,18 @@
 e. If no
-t
+ne
  given, 
@@ -1793,16 +1793,8 @@
  the
- default
 %0A%09 *
@@ -1805,16 +1805,24 @@
         
+default 
 configur
@@ -1846,59 +1846,69 @@
  * @
-param properties%0A%09 *            Optional properties
+see HibernateUtil#registerAnnoteatedClass(Configuration)%0A%09 * 
 %0A%09 *
@@ -2015,31 +2015,8 @@
 urce
-, Properties properties
 ) %7B%0A
@@ -2173,78 +2173,239 @@
 ;%0A%09%09
-if (properties != null)%0A%09%09%09configuration.addProperties(properties);%0A%09%09
+registerAnnoteatedClass(configuration);%0A%09%09return configuration;%0A%09%7D%0A%0A%09/**%0A%09 * Adds the entity model classes into a configuration file that will be used%0A%09 * by Hibernate to map.%0A%09 * %0A%09 * @param configuration%0A%09 */%0A%09public static void 
 regi
@@ -2416,32 +2416,46 @@
 AnnoteatedClass(
+Configuration 
 configuration);%0A
@@ -2448,35 +2448,29 @@
 nfiguration)
-;
+ %7B
 %0A%09%09
-return 
 configuratio
@@ -2466,20 +2466,51 @@
 onfiguration
+.addAnnotatedClass(Event.class)
 ;%0A%09%7D%0A%0A%7D%0A

Simplify HibernateUtil.java