mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 22:16:03 +00:00
add hibernate.cfg and HibernateUtil class
This commit is contained in:
1 parent
ca73621f4d
commit
8208f6252b
2 files changed
+51
No files matched your search
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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