Hibernate Persistence for Beginners

Ladies and Gentlemen! We have finally hit a milestone in our journey of learning Java, we are now done talking specifically about SQL and are moving on to talk about persistence frameworks.

Let’s start with the most obvious question first…

What is a Persistence Framework?

As the name implies, it has something to do with persisting things… this means that we’re still talking about databases. But what is it that we are persisting with this framework?

Objects (of course)

A persistence framework is something we use in Java to bridge the gap between Java and SQL.

Hopefully we’ve gone through all of my previous tutorials and have learned a thing or two about Java and SQL. But one thing I haven’t taught you yet, is how to put your knowledge of SQL into Java.

That’s what I’ll be teaching you throughout these Hibernate (persistence framework) tutorials.

And if you haven’t guessed it already, Hibernate is a persistence framework that you can use in Java. It’s what allows you to write Java code (staying true to Object Oriented programming practices) and yet still be able to communicate with your database. Cool eh?

Why use Hibernate?

Overall, Hibernate is widely adopted and is open source software. This means that you’ll have very readily accessible help documentation across the internet (just like the one you’re reading right now). The more users a particular project/framework has, the more overall support you’ll be able to receive.

Hibernate is no exception to this rule, there are hundreds (if not thousands) of blogs dedicated to Hibernate and Hibernate tutorials.

Not only that, Hibernate is a fairly simple and flexible framework to use when compared to other alternatives out there. You’ll be able to use Hibernate with pretty much any database system you plan on using (including the big ones like MySQL, MS SQL Server, Oracle, PostgreSQL).

Hibernate also integrates and plays nice with the Spring Framework. which is another framework that you’re hopefully already familiar with. If you’re not, then I’d suggest reading up on it.

What you need to know when using Hibernate

There are some pre-requisites for learning Hibernate. I’m going to be progressing through these Hibernate tutorials assuming that you already have at least a beginners grasp on the following languages / technologies:

If you don’t already understand the things listed above, then you’re going to have to pause your learning on Hibernate, because you’ll likely get lost in all the details.

If you have a good understanding of those concepts / technologies, then you’re almost good to go. We’ll need to talk a little bit about a technology called Maven before we can continue.

You see, Maven was created by Apache with the goal of simplifying the annoying project management aspect of programming (namely the build process and library management). As a software project grows, you’re more likely to need to depend on 3rd party software / library (like Spring and Hibernate) to get the job done right.

Getting Spring and Hibernate set up correctly and running is actually a VERY annoying and somewhat complex task… especially since there are so many bloody versions of each framework. This is where Maven comes in, it allows you to MUCH more easily point to the version of each framework that you want to use and then “install it”.

Maven uses something called a “project object model” to manage your projects… this is just a fancy way of saying, they have a single XML file where you will specify all the information about what frameworks you’d like your project to use. It’s not too difficult to use once you get used to it.

The goal of these tutorials, however, is not to teach you the ins and outs of Maven, but rather to just show you the basics of what you need to know about Maven to get up and running smoothly with Hibernate (and Spring).

Okay, Let’s Setup Hibernate

The easiest way for me to show you how to go about setting up Hibernate is to do it in a video, so I’ve recorded one and have included it below for your viewing enjoyment!

Please watch the video above for a full explanation of how to set up your project.

Below I’ve included the source code I used to create the two Hibernate specific set up files.

Hibernate Configuration Files

There are two files that you’ll need to create to get the ball rolling with your Hibernate set up.

PersistenceConfig.java

persistence-mysql.properties

Note: The actual names of these files doesn’t actually matter, what matters is that one is a Java file that will be used to configure your Hibernate framework and a properties file.

Hi:
I do appreciate your hard work putting all this together. I’m refreshing my knowledge about java in order to achieve certification and your podcast are very helpful. Leep the good work, I’m sure lots of people are hearing/reading/watching you even if they don’t post comments here 🙂

Dear Trevor:
Everyone that comes across your tutorials/blogs/podcasts should know that there is no other source online that explains the way you do for free. You explain what Java and all the frameworks are in a way that a even 10 year old can understand. I’m not sure why you are so extremely generous and compassionate to those that want to know Java but initially give up because it’s just too complicated (me) but I hope you become famous one day so everybody benefits like I am. Thank you so much. I will be sure to inform you and your students when I get higher paying job.

Thank you very much. I really liked the way of explanation. I really hated Hibernate for its cumbersomeness, especially given the stereotypes of J2EE. Now I am trying to understand, exactly what problem is a particular framework/solution is trying to solve. Your tutorial is a starting point for it.