Hibernate vs JDBI

rigal picture rigal · Oct 19, 2014 · Viewed 8.9k times · Source

I am building a web service using the Dropwizard framework (version 0.7.0). It involves executing some read-only queries to the database, manipulating the result set and then returning that data set. I am using MySQL as a database engine. Since I am new to this framework, I want to know which option I should choose: Hibernate or JDBI.

Answer

th3morg picture th3morg · Oct 20, 2014

I've used both of these. I've used Hibernate with GORM in Grails as well as in a traditional Spring app and I've used JDBI in Dropwizard.

I have really enjoyed the simplicity of JDBI and here are a couple of reasons why I prefer it over Hibernate.

  1. I know exactly what SQL is going to be executed to acquire the data I'm requesting. With Hibernate, you can sometimes have to do a lot of messing around with HQL and configuring your objects to what you intended to have returned. You ultimately resort to SQL, but then have the difficultly of properly mapping your results back to your domain objects, or you give up and allow hibernate to fetch them one by one.

  2. I don't need to worry about lazy/eager fetching and how that is going to affect my query time on large data sets.

  3. Mappings aren't complicated because you manage them on your own and you don't have to rely on getting the right combinations of annotations and optimizations.

For your case in particular, it sounds like you'd want something lightweight because you don't have a lot of use cases and that would definitely be JDBI over Hibernate in my opinion.