FULL COURSE: Spring MVC and Hibernate (200+ videos)
This Hibernate tutorial series will help you quickly get up to speed with Hibernate.
DOWNLOAD SOURCE CODE for Hibernate tutorial.
View more videos on the playlist, Hibernate Tutorial: https://goo.gl/UKAdaq
Follow luv2code for more Hibernate tutorials:
If you liked my Hibernate tutorial, then join my mailing list: Get exclusive access to new Java tutorials.
Questions or problems about this Hibernate tutorial? Post them in the comments section below.
Want to suggest a video for my Hibernate tutorial? Leave a comment below. I'm always looking for new video ideas.
Let me know what video you'd like for me to create for this Hibernate tutorial.
Hibernate Tutorial Transcript
Hey. In this video, we're going to cover the Hibernate Configuration. Just a checkpoint here on our to do list, the first thing we need to do is set up our Hibernate Config file, annotate the Java class and develop the Java code. Right now, we're at step one of working on our Hibernate Configuration file.
All right, now the configuration file basically tells Hibernate how to connect to the database. Again, remember that Hibernate uses JDBC in the background for communicating with the database. The bulk of the information that we'll have in a config file is that we'll actually have the JDBC configuration. We'll have the database URL, user ID, password and so on just to tell Hibernate how to connect to the database. Then we'll have some of those smaller config items. I'll show you that in a second.
Let's go ahead and move into Eclipse. What we want to do is look at the source files that we downloaded for this tutorial. In our downloads folder, and I'll swing it here onto the screen here for a second, in our downloads folder we have this directory or folder here called "Starter Files." Inside of Starter Files we have a very basic Hibernate configuration file. It's called "HibernateConfig.xml." I'll go ahead and right click this file and copy it, and I'll actually paste it into my Eclipse environment.
When I paste this file in Eclipse, I have to paste it at the root of the source directory. This makes it available for Hibernate to use it, because it has to be on the actual class path of your application. Here I'll paste it here on the source directory. You can put it in other locations, but to keep it simple, I'll just place it here at the root of our source directory. That's our HibernateConfig.xml. We can go ahead and double click this file and open it up and see what's inside.
Here's our file. Near the bottom, click on the tab for "Source" so we can see a source code view of the config file. Let me expand this window here so we can see it full screen. Okay, great, so this is our Hibernate Configuration xml file. We have some parameters here set up for session factory. We'll talk more about session factory in a second. Basically session factory allows us to get session objects for connecting to the database. The most important items here are lines nine through thirteen, where we set up our JDBC database connection settings. Again, we simply go through, we tell Hibernate the actual driver class that we're going to use, the database URL, and the user ID and the password. This all should look familiar to you based on that previous example that we ran for our JDBC test. Again, the connection URL, we simply give local host 3306, HB student tracker, use SSL false. That's it. That basically tells Hibernate how to connect to our MySQL database.
Hibernate also has a very basic JDBC connection pool. For our demos, we'll simply set the connection pool size to one because we'll simply run things as a simple Java app. I'll talk more about connection pooling later on when we move into some more advanced development.
On lines 18 and 19, we set up our SQL dialect. SQL is a standard, however, as we know, each database has its own little twist on it or its own little dialect. Here we're going to make use of the MySQL dialect. When Hibernate generates the SQL, it knows how to talk to MySQL. They have dialects for each of the different databases, like Oracle, SQL Server, PostgreSQL, so on and so forth. This is enough here for connection to MySQL. Then on 21 and 22, I simply set up a configuration to show the SQL so when Hibernate executes, it'll actually print out the SQL that it's going to use when it sends it over to the actual database. This is very useful during development and testing, just to see what Hibernate's doing in the background.
This is the end of the Hibernate tutorial transcript