Repaso
Español

What are the most common pitfalls with JPA/Hibernate and @Transactional?

Answer

  • N+1 queries: LAZY collections accessed in a loop; fix with join fetch, @EntityGraph, batch size or DTO projections.
  • LazyInitializationException: reading a lazy field outside the transaction; fetch what you need inside the service or use DTOs.
  • Self-invocation or private methods: proxy bypassed, so no transaction.
  • Checked exceptions do not roll back by default (rollbackFor).
  • Too-long transactions (calling slow HTTP services inside) hold database connections.
  • Returning entities directly from controllers: leaks fields and triggers lazy loading.
  • open-in-view (default true in Boot) hides lazy loading problems in the web layer; many teams disable it.

Key terms

Practice this question in Repaso