Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Configuring a Project to Use Hibernate6:34 with Chris Ramacciotti
Configuring a project to use Hibernate involves a couple simple steps. In this lesson, you will see how to add the Hibernate dependency to your Gradle build file and include a Hibernate configuration file in XML format.
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 email@example.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.
After reviewing database and SQL basics and 0:00 noodling around a bit with JDBC, you're ready to work with a powerful ORM. 0:03 For the rest of the course, we'll be working in IntelliJ and 0:08 I have the Community Edition installed. 0:11 We'll use Gradle to manage our dependencies, so 0:14 if you haven't seen that workshop yet, check the teacher's notes. 0:16 With IntelliJ Open, let's create a new Gradle project and 0:20 with Java selected I'll go to the next screen. 0:24 The GroupId will be com.teamtreehouse and for 0:27 the ArtifactId I'll use contactmgr-hibernate. 0:31 I'll hit Enter to go to the next screen. 0:36 And I'm going to check this box to have IntelliJ create empty directories for me. 0:39 I'll click Next once again and I will make sure that my path 0:45 reflects the places where I want to store the project and everything looks good. 0:50 So I will click Finish. 0:54 Now in our project the first thing we'll want to do is add or hibernate and 0:56 h2 dependencies. 1:00 H2 is the database vendor we'll be using for the rest of the course. 1:02 So in build.gradle, let's add those dependencies. 1:05 Here's build.gradle and 1:08 I will replace this dependencies block with the ones that I'd like to use. 1:10 Org.hibernate:hibernate-core, 1:15 5.1.0.Final and then also h2. 1:22 So that's com.h2database:h2:1.4.191. 1:28 Let's go ahead and save that file and with our dependencies added to the build file. 1:37 Let's refresh using the Gradle Tool window and here's that Refresh button, 1:44 right here. 1:48 And you should see some messages at the bottom of the screen 1:49 indicating that those repositories are being downloaded. 1:52 Since we will be using a file-based h2database, 1:57 let's create a directory where that will be saved. 2:00 This could be stored anywhere in your file system and feel free to do so, but 2:04 I'll just stick it under the root of the project. 2:07 So I'm gonna right-click here and choose New > Directory and simply name it data. 2:10 The next thing we'll do to set up hibernate is create a configuration file. 2:17 The standard form of this is to use an XML file named 2:21 hibernate.cfg.xml somewhere in the class path. 2:24 Since the resources directory which is located under source main, 2:28 the resources directory is in the class path 2:33 by default when using the great old Java plugin, let's just put it there. 2:35 So I'll right-click resources and 2:40 choose New file naming it again, hibernate.cfg.XML. 2:43 What we'll be placing here are configuration settings for 2:48 the session factory. 2:51 We could instead configure the session factory completely with Java code, but in 2:53 the interest of keeping our configuration settings outside of our source code, 2:58 we'll stick with the XML file here. 3:02 The top level XML element needs to hibernate-configuration. 3:04 So I'll type that name here and just hit tab to get the opening and 3:09 closing hibernate configuration tags in there. 3:14 And inside this element we'll need a session-factory element. 3:18 So again, I'll type that name and hit tab. 3:23 There are three property elements that we absolutely need to include. 3:26 The way we enter them is with a property element that includes a name 3:30 attribute where we'll name the property. 3:33 Then inside the elements we'll specify their values. 3:35 I'll start by adding a couple comments indicating 3:38 the settings that I want to include. 3:41 Comments in XML are just like HTML. 3:43 So here I'll have Database connection settings. 3:47 And then I'll have the SQL dialect. 3:53 Now just as I stated before, we'll need property 4:02 elements that have name attributes just like that. 4:07 I want two under the data base connections, so I'll copy that and 4:12 paste that. 4:15 And then one under the sequel dialect. 4:16 I'll past one there as well. 4:18 In these first two properties, we'll tell hibernate how to connect to our data base. 4:20 And in this third one, we'll tell it the dialect that we'd like it to use. 4:25 Now this dialect will be specific to the database vendor chosen 4:30 since each vendor can have slightly different database features and 4:34 their SQL syntax can vary a bit. 4:38 The first database connection property name is connection.driver_class and 4:40 its value is going to be org .h2.driver and 4:47 you should get some suggestions here as long as h2 is on the class path. 4:52 So we'll drop that driver name in there, and 4:57 then the second connection setting is going to be connection.url. 5:00 If you remember from previously in this course, 5:06 we talked about a connection string. 5:08 This is where you'll place the connection string. 5:10 But in this case, it's going to be jdbc:h2 as opposed to SQL light, 5:12 that we used earlier in the course, followed by 5:17 ./data/contactmgr, and I will leave it just like that. 5:21 Now I started this path here with 5:28 a ./ to indicate to hibernate that this will be a path relative 5:31 to the root of the project which is exactly where my data directory is. 5:36 Just under the root directory of the project. 5:41 If you created this data directory elsewhere, 5:45 then you'll change this path right here to reflect the path of the directory that 5:47 you created when you made your data directory. 5:52 The next property is the sequel dialect that hibernate should use while 5:56 communicating with the database. 5:59 And this is a class that comes from the hibernate library. 6:01 We're gonna slide this over here to get myself a little more room. 6:04 So the name of this is simply dialect. 6:10 And the dialect we'll be using is org.hibernate.dialect.h2. 6:13 There it is right there. 6:21 And at this point, we have our basic configuration set up. 6:24 So let's break here, and 6:28 after a bit more coding, we'll return to this xml file for a couple additions. 6:29
You need to sign up for Treehouse in order to download course files.Sign up