File tree Expand file tree Collapse file tree
java/com/iluwatar/cqrs/util Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .iluwatar .cqrs .util ;
2+
3+ import org .hibernate .SessionFactory ;
4+ import org .hibernate .boot .MetadataSources ;
5+ import org .hibernate .boot .registry .StandardServiceRegistry ;
6+ import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
7+
8+ /**
9+ *
10+ * @author Sabiq Ihab
11+ *
12+ */
13+ public class HibernateUtil {
14+
15+ private static final SessionFactory SESSIONFACTORY = buildSessionFactory ();
16+
17+ private static SessionFactory buildSessionFactory () {
18+ // A SessionFactory is set up once for an application!
19+ final StandardServiceRegistry registry = new StandardServiceRegistryBuilder ().configure () // configures settings //
20+ // from hibernate.cfg.xml
21+ .build ();
22+ try {
23+ return new MetadataSources (registry ).buildMetadata ().buildSessionFactory ();
24+ } catch (Throwable ex ) {
25+ StandardServiceRegistryBuilder .destroy (registry );
26+ // TODO HibernateUtil : change print with logger
27+ System .err .println ("Initial SessionFactory creation failed." + ex );
28+ throw new ExceptionInInitializerError (ex );
29+ }
30+ }
31+
32+ public static SessionFactory getSessionFactory () {
33+ return SESSIONFACTORY ;
34+ }
35+
36+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" utf-8" ?>
2+ <!DOCTYPE hibernate-configuration SYSTEM
3+ "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
4+
5+ <hibernate-configuration >
6+ <session-factory >
7+ <property name =" dialect" >org.hibernate.dialect.H2Dialect</property >
8+ <property name =" connection.driver_class" >org.h2.Driver</property >
9+ <property name =" connection.url" >jdbc:h2:mem:test</property >
10+ <property name =" connection.username" >sa</property >
11+ <property name =" hbm2ddl.auto" >create</property >
12+ <mapping class =" com.iluwatar.cqrs.domain.model.Author" />
13+ <mapping class =" com.iluwatar.cqrs.domain.model.Book" />
14+ </session-factory >
15+ </hibernate-configuration >
You can’t perform that action at this time.
0 commit comments