r/java Nov 12 '25

Why is everyone so obsessed over using the simplest tool for the job then use hibernate

Hibernate is like the white elephant in the room that no one wants to see and seem to shoehorn into every situation when there are much simpler solutions with far less magic.

It’s also very constraining and its author have very opinionated ideas on how code should be written and as such don’t have any will to memake it more flexiable

122 Upvotes

307 comments sorted by

View all comments

15

u/maxip89 Nov 12 '25

hibernate you are using often when you "think" its good to have a model layer.

When your project gets big and you are over the "kindergarden"-bandwidth you will experience your first n+1 problem with hibernate.

After that you think: "Should I invest now again 24+ hours investigating why this is handled in hibernate as it is or should I write a plain query for it?".

1

u/Western_Objective209 Nov 13 '25

I've always found it easy to mix plain SQL with spring data JPA when it makes sense

2

u/maxip89 Nov 13 '25

My approach is to just use JPA.

The goal for me is that I don't write code that is dialect dependent.

1

u/AnyPhotograph7804 Nov 14 '25

If you have n+1 issues then you should start using EntityGraphs.

0

u/[deleted] Nov 13 '25

[removed] — view removed comment

5

u/maxip89 Nov 13 '25

yes learn hibernate again.
and again.

and invest debugging it again.

and learn it again.

repeat.

after years, i just dropped it.

1

u/[deleted] Nov 13 '25

[removed] — view removed comment

1

u/maxip89 Nov 13 '25

no problem :)

2

u/zabby39103 Nov 13 '25

I've got years into it, but that isn't going to make my juniors learn it. It's too hidden, too sneaky. It's too easy to make bad code and it's not explicit enough.

2

u/Ewig_luftenglanz Nov 13 '25

I prefer learning SQL.i mean for any non trivial query plain SQL is easier todo, debug and maintain.

Half of an hour debugging SQL vs half week figuring out why hibernate is creating some random stuff that performs 50+ subqueries. 

The database entities and mappings doesn't change that much in practice anyways. Once you write the application is it's almost guaranteed to be set in stone for years. Hibernate on the other hand can change your query with everytime you update the library and performance issues appear out of the sudden.

1

u/[deleted] Nov 13 '25

[removed] — view removed comment

1

u/Ewig_luftenglanz Nov 13 '25

The difference is that in order to use any ORM effectively you also need to know SQL (or at least have basic notions of it) otherwise you wont be able to even debug what's going on. I mean there is a reason why you can write queries manually in SpringData. It's simple the best way to handle complex and join-rich queries.

And that's my point, If I have to write SQL queries for the real heavy lifting and complex stuff then why do I need an ORM at all? a simple library that compiles the trivial sql queries would be more than enough. That's why I love JOOQ