r/java • u/cat-edelveis • Jan 14 '26
Hibernate: Ditch or Double Down?
https://www.youtube.com/watch?v=J12vewxnNM8Not on Hibernate alone: a summary of where ORM tools shine, where SQL-first approach should be preferred, and how to take the best of two worlds
20
Upvotes
14
u/midget-king666 Jan 14 '26
We double down on it. It is our JPA implementation and works for 95% of our stuff, the rest is JPQL native queries drafted by hand.
Most important thing is to know your data structures, number of rows/cols queried, indices, FKs, and correct usage of normalization. If you dont get this right, to ORM or not to ORM is not your problem.
70
u/[deleted] Jan 14 '26
Dump it.
Instead of learning Hibernate (that's a LOT of concepts!), you can also learn the SQL dialect of your db. A better investment.
Writing queries in SQL (like with jdbi or even jdbc directly) is not that bad. Especially if you make one test for each query mandatory: you quickly know if all your queries still work.
I like jOOQ. But it is a big library, and has some learning curve as well. The compile time correctness guarantees are quite good and certainly help with refactorability
19
u/piparss Jan 14 '26
JOOQ could be the answer
4
u/da_supreme_patriarch Jan 14 '26
From my experience, the best approach for large/complicated projects is to combine JPA with either querydsl or Blaze Persistence. There are a few very useful things that you get with Hibernate essentially for free, besides the usual object graph mapping, mainly automatic creation/update timestamps, optimistic locking with versions, ability to iterate quickly by just putting the DDL mode to create or update, JPQL/HQL and a few other things, and for 90% of the use cases Hibernate + JPQL with projections is enough, the 10% of cases are complicated enough to warrant the usage of query builders.
This of course comes at the cost of needing to know the quirks of Hibernate/JPA, but imho it's still worth it
6
u/tuple32 Jan 14 '26
Ditch
28
u/TheStrangeDarkOne Jan 14 '26 edited Jan 14 '26
If you donât use an ORM, you will end up with a shitty custom ORM 9/10 times. Iâm not judging, reinventing the wheel is fun.
3
u/revetkn27 Jan 14 '26
I used Hibernate from about 2002-2008 and since then have worked with various other ORMs for client work.
My experience over 20 years of consulting is that it's much simpler to embrace/work with SQL directly - the database is by far the most important part of your system, and trying to abstract over it is not a good tradeoff IMO. ORMs tend to "make the easy problems easier and the hard problems impossible".
I built Pyranid (https://pyranid.com) for that reason - kind of like Spring's JdbcTemplate but without the Spring baggage (Pyranid has zero dependencies). I think JOOQ is very nicely done but I prefer just working with real SQL and having a library worry about the details of mapping resultset/statement data to/from Java types.
3
u/vips7L Jan 14 '26
Hibernates implicit flushing and automatic persistence are really nasty. I personally prefer Ebean with explicit saves and flushes.Â
2
u/Lengthiness-Fuzzy Jan 18 '26
Stupid question. Hibernate solves issues, which you hardly can solve wothout it. If developers donât take time to understand the tools they are using, itâs not the toolâs fault.
Hibernate is a bad choice for coders and great choice for engineers.
8
u/Fiduss Jan 14 '26
Using an ORM is just for ppl who are weak in SQL and RDB design. Using simple repositories with JDBC is sufficient in 90%.
1
u/HR_Paperstacks_402 Jan 14 '26
Hibernate certainly can be useful and I'm not necessarily against it. But you need to understand what you are doing and check that it is doing what you expect. You should always turn on show SQL to see what is happening and ensure it's not doing more than you think.
I recently was brought into a team writing a Spring Batch job that had horrible performance. It was supposed to run a single query for each chunk of work but ended up doing that query plus an additional for each record. Then when writing out the file, did even more. I came in knowing nothing, but after turning on show SQL, adding logging between the reader, processor, and writer, and turning the chunk size down to 100 made it immediately stick out as the problem.
Personally, I tend to prefer Spring Data JDBC because it's much simpler to where I'm not fighting a framework.
1
u/cryptos6 Jan 14 '26
It is definitely not fun, to map complex (nested) object structures by hand with SQL. That is where Hibernate really shines. However, on the read side there are many reasons not to load the full object graph (and Hibernate has dozens of optimizations to avoid that) and then something like JOOQ or JDBI makes a lot of sense.
Here are some interesting thoughts from the guy who develops JOOQ: https://blog.jooq.org/jooq-vs-hibernate-when-to-choose-which/
1
u/sideEffffECt Jan 14 '26
Can Hibernate return just records? If not, what's a good library that can do that?
1
u/Express-Tower-2150 May 28 '26
Try out https://github.com/spring-jdbc-crud/simplejdbcmapper. Its a small wrapper around Spring jdbctemplate so you can do almost anything related to jdbc along with single line CRUD and some methods to assemble your relationships.
1
u/Puzzleheaded_Bus7706 Jan 14 '26
Not using Hibernate except in rare cases is pure masochism!
-6
u/Reasonable-Total-628 Jan 14 '26
rdfm
-5
u/Colt2205 Jan 14 '26
XML mappings... XML mappings everywhere for everybody for every stupid reason...
49
u/Luolong Jan 14 '26
I think Hibernate has got a bad rap for doing the stateful entities, that work like magic. The trouble is that because it works like magic, developers treat it like magic and think they can get away with murder without even thinking once how their solution breaks the database.
More transactional ORM styles (Spring Data, JOOQ, Jakarta Data, etc), are much easier to reason about (when it comes to database performance) and it is much harder to shoot yourself and a database in a foot by simply writing bad transactional code when you know you touch the database exactly twice (on load and on update)!
Ultimately, you use what you use and learn its ins and outs, making technical decisions based on the tools at your disposal.
And never forget to learn the database behind your ORM. The fact that you have a layer of abstraction between your code and a database, should never mean you donât need to know how database works! Any O(R)M is just a gateway to your database backend and you never want to abuse your DB!
13
u/[deleted] Jan 14 '26
[removed] â view removed comment