add hibernate.cfg and HibernateUtil class

This commit is contained in:
Sabiq Ihab
2017-06-20 21:35:45 +00:00
parent ca73621f4d
commit 8208f6252b
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.iluwatar.cqrs.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
/**
*
* @author Sabiq Ihab
*
*/
public class HibernateUtil {
private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings //
// from hibernate.cfg.xml
.build();
try {
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Throwable ex) {
StandardServiceRegistryBuilder.destroy(registry);
// TODO HibernateUtil : change print with logger
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return SESSIONFACTORY;
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">sa</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
</session-factory>
</hibernate-configuration>