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, I'm going to give you an overview of Hibernate.
We'll cover the following topics. First off, we'll ask the question, what is Hibernate? Then, we'll go through and look at the benefits of using Hibernate, and finally, I'll show you some real quick code snippets on how to use Hibernate in an application.
All right, so first off, what is Hibernate? Basically, Hibernate is a framework for persisting, or saving Java objects into a Database. It's a very popular framework used by a lot of enterprise Java projects. You can download it for free from Hibernate.org. I'll cover all the downloading portion of it later in another video. Basically, at a very high level, you'll have your Java application, it'll make use of this Hibernate framework and you can use it for saving and retrieving data from the Database.
What are the benefits of Hibernate? Basically, Hibernate handles all of the low level SQL code. It actually minimizes the amount of JDBC code you have to develop. Hibernate actually provides the object to relational mapping and it makes it really easy to create apps that store and retrieve objects from the Database.
How does it work? Again, Hibernate provides something called the object to relational mapping or you'll hear the buzzword or keyword called ORM. As a developer, you need to tell Hibernate how your Java class, or object, how it maps to data in the Database. In effect, you're going to map your Java class to a given Database table.
In this example, on the far left we have our Java class that's a student and has four fields, ID, first name, last name, and e-mail. Note how first name and last name is spelled as far as the camelcase. We have the Hibernate framework in the middle and then on the far right we have the actual Database table. In this example, we have a table called student, it has an ID which is the primary key. There's a first name, last name, and note it's first_name, last_name , and then a field for an e-mail address. What we'll do is we'll tell Hibernate, hey, this Java class student maps to this given table and you set up the one to one mapping between the fields and the actual columns in the Database.
Now, you can set up this mapping via a configuration file using XML or you can set it up using Java annotations and I'll cover all those technical details later in some following videos. For now, we'll just go ahead and say that there's a mapping available for mapping this class to a given Database table.
Okay, great. Let's look at a quick example on how to save a Java object with Hibernate. The first thing we do is we create the Java object and that's standard Java, right? We just use the new keyword, new student, John Doe, [email protected]
That's the first name, last name and e-mail address. Then what we do is we actually save this Java object to a Database. Here we make use of session, which is a special Hibernate object. We say session dot save and then we pass in our object.
What happens in the background is that Hibernate will take that Java object based on those mappings that have been defined earlier, Hibernate will take that information and store it in the appropriate table in the appropriate columns. Hibernate will do all of that work for you, which is really, really cool. If you remember back in the old days of JDBC, you would manually have to write the SQL code, manually set those values and manually execute that SQL statement. Here, Hibernate does all of that work for you which is really cool. Once you do the session dot save, then Hibernate will return the actual ID that's been assigned to that entry. This is actually the primary key. We can use that ID later if we wanted to actually retrieve that object from the Database. As you can see, it's really, really simple here to actually save a Java object using Hibernate.
[snip] Click More - Transcript for details
End Hibernate Tutorial Transcript