Files
sdjpa4-demo/jdbc-vs-jpa/docs/output/01-lazy-outside-session.txt
T
asmhatre 448c383879 Add jdbc-vs-jpa module: Spring Data JDBC vs JPA on a shared Order aggregate
Same Customer/Order/OrderItem domain modelled with Hibernate/Spring Data JPA and
Spring Data JDBC side by side, both running through a shared StatementLoggingDataSource
so SQL-statement counts are directly comparable. 11 tests, 11 captured transcripts,
6 doc chapters. Companion repo for the ankurm.com article on when to drop the ORM.
2026-09-17 19:09:35 +00:00

9 lines
576 B
Plaintext

Scenario: load an Order inside a transaction, return it, then touch the lazy
items collection AFTER the transaction (and its Hibernate session) has closed.
order.getItems().size() -> org.hibernate.LazyInitializationException: Cannot lazily initialize collection of role 'com.ankurm.jdbcvsjpa.jpa.Order.items' with key '34' (no session)
This is the LazyInitializationException every JPA tutorial warns about and few
show you actually triggering. It is not a bug: the collection genuinely has not
been read from the database yet, and the session that could read it is gone.