add hibernate.cfg and HibernateUtil class
This commit is contained in:
36
cqrs/src/main/java/com/iluwatar/cqrs/util/HibernateUtil.java
Normal file
36
cqrs/src/main/java/com/iluwatar/cqrs/util/HibernateUtil.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
15
cqrs/src/main/resources/hibernate.cfg.xml
Normal file
15
cqrs/src/main/resources/hibernate.cfg.xml
Normal 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>
|
Reference in New Issue
Block a user