Hibernate Tips by Thorben Janssen

Hi Thorben,

Tell me a little bit about yourself.

Hi Petri,

thanks for having me.

I have been a software developer and architect for more than 15 years with a strong interest in Hibernate, JPA, and other persistence technologies. In the last few years, I wrote about JPA and Hibernate on my blog, spoke at conferences and offered open classroom and online training about Hibernate.

I’m also the author of the Amazon bestselling book Hibernate Tips - More than 70 solutions to common Hibernate problems.

A lot of people know you as a Hibernate expert. Why did you decide to become one?

To be honest, that happened over time and more or less by accident. I always have been interested in the typical Java backend technologies. Hibernate, as one of the most popular JPA implementations, was obviously a part of that.

As a developer, I used Hibernate in lots of projects with very different requirements. Sometimes, I had to handle huge amounts of data or parallel requests, and sometimes I just had to implement a small application as fast as possible.
As I learned during the years, Hibernate helps you to implement both kinds of applications. But you need a good understanding of your requirements and the different Hibernate features.

At the end of 2013, I started my blog with a few posts about JPA 2.1. I quickly recognized that a lot of other developers were interested in the same topic. So I kept writing and learning about JPA and Hibernate.

Now, a few years later, I wrote more than 100 posts about JPA and Hibernate, worked on lots of interesting projects, spoke at conferences, offered several online, on-site and open classroom trainings and wrote a book about Hibernate. And at some point, people started to call me an expert ;-)

What is the most common misconception that developers have about Hibernate?

That depends if the developer likes to use Hibernate or not.

A common misconception in the group of developers who are using Hibernate is that Hibernate provides a good solution for everything. As with every piece of software, that’s not the case.

Hibernate and JPA make the implementation of most create, update and delete operations very easy and efficient. But if you need to implement a lot of complex queries, Hibernate is most often not the best choice. You can, of course, use native SQL queries with Hibernate. But if you need a lot of them, you should take a look at other frameworks, like jOOQ or QueryDSL, which make this a lot easier.

Another common misconception is that Hibernate would be too slow for applications with high-performance requirements. This one is especially popular in the group of developers who don’t like to use Hibernate.

In my experience, that’s most often not the case. Hibernate makes it easy to implement a persistence layer without understanding how Hibernate interacts with the database. That makes it easy to create an inefficient application. Even when the database access seems to be the bottleneck, most of the performance problems could be avoided with a better knowledge about Hibernate or the used database.

Hibernate offers lots of optimizations that reduce the number of required queries and due to this, improve the performance of your application. Good examples for that are:

  • The write behind optimization, which groups multiple update operations into one.
  • The SequenceGenerator which uses the hi-lo algorithm by default to avoid an additional SQL statement to get the primary key value for each new record.
  • The first-level, second-level and query cache that can store a lot of information in local memory to avoid unnecessary database queries.

You published your first book titled: “Hibernate Tips” earlier this week. Why did you decide to write it, and what problem does it solve?

I wrote Hibernate Tips - More than 70 solutions to common Hibernate problems as a cookbook to help my readers to use Hibernate in their daily work. It uses a FAQ or problem-solution approach to show how to implement different tasks with Hibernate. Here you can take a look at a few example chapters.

It all started in 2016 with an idea for a series of blog posts. I got similar questions from coworkers and blog readers. They knew which general concepts they had to apply to implement their current task but not the Hibernate annotations and APIs they had to use. Or they were wondering about the best way to implement a common task. And as probably every developer, I knew these situations just too well.

To answers these questions, I started to write a series of blog posts. Each post answered a specific question with a short explanation and a few code snippets. I quickly recognized that a lot of readers liked this format and that it would also be a good fit for a book.

So, I started to outline the book and announced it in January 2017. Within the following 3 months, I wrote more than 35 tips exclusively for the book and edited the existing posts. The book now provides you with more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.

As you can see, it covers a broad range of topics so that it’s a good fit for developers of all skill levels, as long as they’re familiar with general Hibernate and JPA concepts. For just a few days, you can get it at a special launch price of just $2.99 (ebook) or $12.99 (paperback) on hibernate-tips.com.

I have noticed that a lot of people are looking for alternative ways for implementing their repositories. What I mean is that people seem to be replacing ORMs with plain old JDBC. Do you have any thoughts about this?

You’re probably not surprised when I tell you, that you shouldn't use plain JDBC. That API is difficult to use and has lots of pitfalls that you can easily avoid by using one of the higher-level frameworks.

But that also doesn’t mean that I recommend using Hibernate for all of your projects. It’s a good framework for a lot of projects, especially if you need to implement many create, update or delete operations. If that’s not the case, there are several other libraries, like jOOQ or QueryDSL, which provide better support for complex queries. If you’re not sure if Hibernate is a good fit for your projects, you should take a look at this post and questionnaire in which I guide you through the decision.

And when you decide that Hibernate is a good fit for your project, you should take a look at my new book Hibernate Tips - More than 70 solutions to common Hibernate problems. It provides you with more than 70 ready-to-use recipes to help you implement your persistence layer with ease. For just a few days, you can get it at a special launch price of just $2.99 (ebook) or $12.99 (paperback) on hibernate-tips.com.

1 comment… add one

Leave a Reply