Building a Hibernate SessionFactory4:41 with Chris Ramacciotti
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`.
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
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 firstname.lastname@example.org: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.
All right, with our configuration out of the way and our entity class coded and 0:00 annotated, it's time to fire up a session factory. 0:04 Along the way we'll need to create some intermediate components. 0:08 Let's get started and we'll chat about each component as we code it. 0:11 I'm in Application.Java now where we'll create our session factory. 0:14 And since we'll only be needing one session factory, 0:20 we'll define it as a static final member. 0:22 So here, I will hold a reusable reference 0:24 to a SessionFactory, since we need only one. 0:30 Okay, as I said, we declared as a private static final member. 0:39 And this will be a hibernate SessionFactory, there it is right there. 0:44 And I'll name it simply SessionFactory. 0:48 And into it, I'll store the return value of a buildSessionFactory method. 0:51 Of course, we have no build session factory method so let's create that now. 0:58 This method will also be a private static method and 1:03 will return a SessionFactory and I'll name it buildSessionFactory. 1:06 Just as a reminder, 1:12 static members are always loaded as soon as the class is needed by the JVM. 1:13 Therefore, before the main method is called by the JVM, the session factory 1:17 field is initialized according to our code by calling the buildSessionFactory method. 1:22 The first component we'll create as a standard service registry object, 1:28 which will give us access to hibernate main services, 1:31 including jbvc connectivity, hibernate configurations via xml, 1:35 importing initial databases data from a SQL file if we wanted to and 1:40 the thing we're ultimately after in this method, building a session factory. 1:43 Let's see how this looks and then I'll explain what it does. 1:47 So in my comment here, I'll say that we'll create a StandardServiceRegistry object. 1:50 So, I'll declare this as final, and as a ServiceRegistry object. 1:58 I'll name it registry, and I'll start by creating a new 2:03 StandardServiceRegistryBuilder. 2:07 And on this object, I'll call its configure method and 2:14 then on this object I will call its build method. 2:19 Now let me explain what's happening here. 2:23 The first part creates a builder object that's used to create 2:26 the StandardServiceRegistry. 2:29 That's the StandardServiceRegistryBuilder object. 2:30 More on this builder pattern in a bit. 2:34 Notice that I'm storing it into registry, 2:37 which is declared using the service registry interface. 2:40 Calling the configure method on the builder loads the hibernate 2:44 configuration file from its default location Which is a file name 2:48 hibernate.cfg.xml on the classpath, which in our application exists. 2:53 Now a different location for 2:59 that file could be specified here by including a string parameter. 3:00 But we'll leave it as the default since that's where our file exists. 3:04 Finally a call to this build method will construct 3:08 a standard service registry object. 3:11 The next object we'll need to build is the metadata sources object 3:14 which is the manner by which we can start loading the JPA 3:17 annotated anodised that we reference in the hibernate config file 3:20 which is simply the contact anody in our case. 3:24 So I'll create a new MetadataSources object, 3:27 there it is, and I'll pass to it registry I created in the line above. 3:31 And we'll use the metadata sources object we just created to build the metadata 3:37 object itself, which is the thing that encapsulates 3:41 all the ORM mappings loaded from the annotated entities. 3:44 And to do that I will call the buildMetadata method. 3:48 And, finally, we'll use this metadata object that 3:52 is returned by the buildMetadata method to build to build the session factory, 3:56 which is the one object that encapsulates all the configuration of all our mappings, 4:01 as well as the configuration for how to connect to the database itself. 4:06 And this is the object that we'll need to return. 4:10 So we'll build the SessionFactory, and that's the object that we need to return. 4:12 Notice how this uses a similar builder pattern as a service registry. 4:17 We have this buildMetadata, this buildSessionFactory, 4:22 just like we did up above, where we had the StandardServiceRegistryBuilder 4:25 which ends with the call to the build method. 4:30 We'll take a look at this builder pattern next, but for 4:33 now, know that you've successfully created a hibernate SessionFactory. 4:36
You need to sign up for Treehouse in order to download course files.Sign up