Building a Hibernate SessionFactory

Before you can fully leverage the power of Hibernate, you will need to create the one object that encapsulates all of our mappings and also includes all information needed to connect to the database. The object of focus for this video is a Hibernate `SessionFactory`.

To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.

Clone this repository to your machine using the Github desktop client, or using the following command:

git clone git@github.com:treehouse/contactmgr-hibernate.git

To update your local repository to match a video's starting point, you can use the git checkout ... command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:

git checkout -f s5v4

Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.

0:00

All right, with our configuration out of
the way and our entity class coded and

0:04

annotated, it's time to
fire up a session factory.

0:08

Along the way we'll need to create
some intermediate components.

0:11

Let's get started and we'll chat
about each component as we code it.

0:14

I'm in Application.Java now where
we'll create our session factory.

0:20

And since we'll only be
needing one session factory,

0:22

we'll define it as a static final member.

0:24

So here, I will hold a reusable reference

0:30

to a SessionFactory,
since we need only one.

0:39

Okay, as I said, we declared as
a private static final member.

0:44

And this will be a hibernate
SessionFactory, there it is right there.

0:48

And I'll name it simply SessionFactory.

0:51

And into it, I'll store the return
value of a buildSessionFactory method.

0:58

Of course, we have no build session
factory method so let's create that now.

1:03

This method will also be
a private static method and

1:06

will return a SessionFactory and
I'll name it buildSessionFactory.

1:12

Just as a reminder,

1:13

static members are always loaded as
soon as the class is needed by the JVM.

1:17

Therefore, before the main method is
called by the JVM, the session factory

1:22

field is initialized according to our code
by calling the buildSessionFactory method.

1:28

The first component we'll create as
a standard service registry object,

1:31

which will give us access
to hibernate main services,

1:35

including jbvc connectivity,
hibernate configurations via xml,

1:40

importing initial databases data
from a SQL file if we wanted to and

1:43

the thing we're ultimately after in
this method, building a session factory.

1:47

Let's see how this looks and
then I'll explain what it does.

1:50

So in my comment here, I'll say that we'll
create a StandardServiceRegistry object.

1:58

So, I'll declare this as final,
and as a ServiceRegistry object.

2:03

I'll name it registry, and
I'll start by creating a new

2:07

StandardServiceRegistryBuilder.

2:14

And on this object,
I'll call its configure method and

2:19

then on this object I will
call its build method.

2:23

Now let me explain what's happening here.

2:26

The first part creates a builder
object that's used to create

2:29

the StandardServiceRegistry.

2:30

That's the StandardServiceRegistryBuilder
object.

2:34

More on this builder pattern in a bit.

2:37

Notice that I'm storing it into registry,

2:40

which is declared using
the service registry interface.

2:44

Calling the configure method on
the builder loads the hibernate

2:48

configuration file from its default
location Which is a file name

2:53

hibernate.cfg.xml on the classpath,
which in our application exists.

2:59

Now a different location for

3:00

that file could be specified here
by including a string parameter.

3:04

But we'll leave it as the default
since that's where our file exists.

3:08

Finally a call to this
build method will construct

3:11

a standard service registry object.

3:14

The next object we'll need to build
is the metadata sources object

3:17

which is the manner by which
we can start loading the JPA

3:20

annotated anodised that we reference
in the hibernate config file

3:24

which is simply the contact
anody in our case.

3:27

So I'll create a new
MetadataSources object,

3:31

there it is, and I'll pass to it
registry I created in the line above.

3:37

And we'll use the metadata sources object
we just created to build the metadata

3:41

object itself,
which is the thing that encapsulates

3:44

all the ORM mappings loaded
from the annotated entities.

3:48

And to do that I will call
the buildMetadata method.

3:52

And, finally,
we'll use this metadata object that

3:56

is returned by the buildMetadata method
to build to build the session factory,

4:01

which is the one object that encapsulates
all the configuration of all our mappings,

4:06

as well as the configuration for
how to connect to the database itself.

4:10

And this is the object
that we'll need to return.

4:12

So we'll build the SessionFactory, and
that's the object that we need to return.

4:17

Notice how this uses a similar
builder pattern as a service registry.

4:22

We have this buildMetadata,
this buildSessionFactory,

4:25

just like we did up above, where we
had the StandardServiceRegistryBuilder

4:30

which ends with the call
to the build method.

4:33

We'll take a look at this
builder pattern next, but for

4:36

now, know that you've successfully
created a hibernate SessionFactory.