Ever looked for a comprehensive tutorial to Hibernate and JPA that is fun and entertaining at the same time? The video tutorial below is a crash course into the Hibernate and JPA universe. From mapping annotations and
SessionFactory setup to HQL and criteria queries, we cover all the basics you’ll need to get started with Hibernate and JPA.
What’s in the Video?
There are so many terms when it comes to Hibernate: Hibernate (i.e., JPA, HQL, JPQL, and SQL) that we first have to understand what Hibernate is and what problem it tries to solve. The core problem is mapping between Java classes and relational database tables.
Afterward, we’ll jump right into the project. You can choose to clone the sample project to follow along with this video, and we’ll dive deeper into what dependencies you need to get started with Hibernate. Hint: It’s not that many. In fact, it’s only one: even though modern stacks like Spring Boot might make you believe differently.
Once we have the dependencies sorted out, we’ll have a look at Hibernate’s mapping annotations. One of the core ways to let Hibernate know about your database tables is through annotations such as
@Column, and many more. We’ll play with those. We’ll also figure out that one of the most popular Hibernate books spends roughly 200 pages on mapping annotations. A lot of work you have to go through yourself, which cannot be condensed into just a video.
Having the dependencies and annotations set up correctly means we can now go about creating a
SessionFactory essentially is Hibernate. It’s the core class you want to use whenever you want to, for example, fire queries against a database, or simply just save one of your objects.
SessionFactory is up and running, we can start considering how to make basic CUD queries work. We can create, update, and delete, but not yet send queries. Hence, we’ll use the
SessionFactory to do just that, save and update a couple of users we had previously created.
Let’s add an “R” to CUD and make it fully CRUD by executing HQL queries against the database. HQL queries are Hibernate’s way of querying database tables, though you don’t write your HQL against specific database tables; rather, against your mapped object hierarchy. At the end of the day, Hibernate will transform the HQL into SQL for you and you’ll see it printed out in the console.
Up next, let’s solve the big question of what JPA, the Java Persistence API actually is. By just swapping out a couple of classes, we can actually turn our application from a Hibernate application into a JPA application. We’ll also talk about the real-life implications this actually has.
Every project, sooner or later, wants to create dynamic SQL queries. Hibernate/JPA’s way of doing that is through its Criteria API. It’s a bit cumbersome to use, but it does the job. Again, it’s several dozen pages in the official documentation, but we’ll try our best and cover the core concepts of the Criteria API.
Before we come to a close, we’ll also spend some time figuring out where Hibernate/JPA fits into the Spring Boot universe and how it compares to other projects, such as Spring Data JPA.
By the end of this video, you should have a good grasp of basic Hibernate and we’ll finish by giving you some tips on what to learn next to become a Hibernate expert.