r/SpringBoot • u/codingwithaman Senior Dev • Jun 24 '26
Discussion 8 JPA/Hibernate mistakes in Spring Boot that can cause issue in production
These are errors I keep seeing in code reviews after years of working with Spring Boot. None of them throw errors during development. All of them cause problems at scale.
The N+1 you dont know about. findAll() returns 100 orders. Each has a customer. Hibernate fires 1 query for orders plus 100 queries for customers. 101 round trips for what should be 1. Fix with JOIN FETCH or EntityGraph.
save() fires a SELECT before every INSERT when using UUID as ID. ID is never null so Spring thinks entity exists. Calls merge() which does a SELECT first. 10000 inserts become 20000 queries. Fix by implementing Persistable and overriding isNew().
Returning entities from controllers. Jackson calls getters during serialization. Lazy relationships trigger N+1 queries while writing the response. Always return DTOs.
Using Lombok Data on entities. Generates equals/hashCode using all fields including ID. ID is null before save, has value after. HashSet breaks. Use Getter Setter and write equals/hashCode on a business key.
Batch inserts dont work with GenerationType.IDENTITY. IDENTITY needs a round trip per INSERT to get the generated ID. Batching impossible. Use SEQUENCE with allocationSize 50.
deleteAll() loads everything into memory first. 50000 rows means 50000 SELECT then 50000 individual DELETE. Use Query with bulk delete instead.
Batching is disabled by default. Every save() is one individual round trip. Set hibernate.jdbc.batch_size to 50 with order_inserts true. Difference between a 4 hour import and a 5 minute one.
Caching entity references. Default cache stores references not copies. One request mutates the cached entity. Every user after that sees corrupted data. Cache DTOs or records instead.
Bonus. Set spring.jpa.open-in-view to false. Default is true. Keeps Hibernate session open during JSON serialization. Hides lazy loading issues that explode in production.
What JPA gotchas have you experienced in production?
21
u/e5disconnected Jun 24 '26
Another reason why you should never use lombok Data on entities (your point 4) is that equals/hashcode on all fields also includes related entities which means that simply putting that entity in HashSet can trigger select queries in DB since lombok's equals will trigger lazy loading
9
u/BrownBurger- Jun 24 '26
Great post thanks
9
u/youwillnevercatme Jun 25 '26
What a breath of fresh air with this nice post in a sea of ai slop articles and posts everywhere. Thank you!
4
u/theatrical33 Jun 24 '26 edited Jun 24 '26
> Set hibernate.jdbc.batch_size to 50 with order_inserts true.
Just hope it does not break 10 unrelated things in the existing application. I'd love to.
7
u/Competitive_Shoe_114 Jun 24 '26
I always find it weird to have to do these things in order to not have performance issues.
It's like saying : "here is your air conditioner to refresh your room when it's really hot outside ! But if the temperature outside is greater than 34°C, you have to plug 2 fans near your air conditioner to prevent him from over-heat, and you can't set the temperature below 25°C".
On a more serious note :
For the points 1. 2. .4 and 6. , can you provide some examples because i don't understand and/or vizualize what it means in the code ?
3
u/DamienDoesItBetter Jun 24 '26
Nice. Ran into 2-3 of this problems in my first 6 months into the job.
8
u/disposepriority Jun 24 '26
Opting out of Hibernate is a super power
2
u/soft_white_yosemite Jun 24 '26
I’m printing these out!
Thank you!
19
u/carlashnikov_92 Jun 24 '26
The more I use JPA, the more I prefer writing raw queries with JDBC or just using jOOQ.
9
u/oweiler Jun 24 '26
2. Or better: UseGenerationType.UUID.
5
u/SHAeronex Jun 25 '26
I think the worst by far is returning entities from endpoint.
I've seen many horror codes during my young 10 years carreer but I think i would throw my keyboard through my work place aiming for the eyes of the little shit that would do that.
1
u/auspis-23 Jun 24 '26
Idempotence mismatch... I build my own solution and don't use JPA
1
u/vEnoM_420 Jun 25 '26
Nice to see you posting on reddit :)
I bought your spring boot course a while back, but haven't been able to finish it. This weekend I aim to progress it 🚀
2
u/11timesover Jun 25 '26
Very useful post. Thank you.
1
u/l3g4tr0n Jun 27 '26
i was expecting another bs tips, but these are actually great, as i already experienced problems related to these in production and i told myself "i wish i knew it sooner"
1
u/wholesale-chloride Jun 29 '26
I just ran into #5 last week. It was so disappointing because I love identity columns and hate sequences.
1
u/kolocolo Jun 29 '26
Ive got an entity with ID setup like in the code snippet below and I don't experience issue from point 2)
1
u/JuiceChance Jul 10 '26
ORM is the biggest idiocy that has ever happened.
1
u/p3t-code Jul 15 '26
IMHO there is also the pitfall with the `Pageable` fetching all the time the total count, as well as all records before the requested page: https://docs.spring.io/spring-data/relational/reference/jdbc/query-methods.html.
Acceptable for small table sizes but with tables having many records you need to find another strategy (in case you want to fetch them all, page-by-page).
-2
u/[deleted] Jun 25 '26
Fuck Java dude