Quick revision
The short answer
LazyInitializationException means a lazy association was touched after its persistence context closed; we fix it by fetching what we need inside the transaction (JOIN FETCH, an entity graph or a DTO projection), not by enabling open-in-view or enable_lazy_load_no_trans. MultipleBagFetchException means one query tried to JOIN FETCH two List collections, which would multiply rows; we fetch one collection per query or use Set where its semantics fit. TransientPropertyValueException means an entity references a new, unsaved entity without cascade; we persist that entity first or add cascade PERSIST where the parent clearly owns it.
What to remember
In this answer, we saw three errors and their real causes. LazyInitializationException comes from touching a lazy association after the Session closed, and is fixed with JOIN FETCH, entity graphs or DTO projections rather than open-in-view or enable_lazy_load_no_trans, which cause N+1 queries and long-held connections. MultipleBagFetchException comes from fetching two bags into a Cartesian product and is fixed by fetching one collection per query or using Set where it fits. TransientPropertyValueException comes from referencing an unsaved entity and is fixed by persisting it first or by cascading where ownership is clear.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
LazyInitializationException means a lazy association was touched after its persistence context closed; we fix it by fetching what we need inside the transaction (JOIN FETCH, an entity graph or a DTO projection), not by enabling open-in-view or enable_lazy_load_no_trans. MultipleBagFetchException means one query tried to JOIN FETCH two List collections, which would multiply rows; we fetch one collection per query or use Set where its semantics fit. TransientPropertyValueException means an entity references a new, unsaved entity without cascade; we persist that entity first or add cascade PERSIST where the parent clearly owns it.
Why this question is asked
These three exceptions are among the first that every JPA developer meets, usually on the first real feature. Each one has a quick fix that makes the error go away and creates a worse problem later, which is exactly why interviewers ask about them: they want to hear the cause and the right fix, not the configuration switch. In this answer, we will recap the persistence context and lazy loading, then take each error in turn, reproduce it, explain why Hibernate refuses, and fix it properly. Along the way we will see why open-in-view and enable_lazy_load_no_trans hide the first error at a real cost.
1A Quick Recap: Sessions, Proxies and Bags
The persistence context, which Hibernate calls the Session, is the set of entities loaded in the current unit of work, usually one transaction. A lazy association is not loaded with its owner. Instead Hibernate puts in a proxy, a placeholder object that loads the real data the first time we use it, or an uninitialized collection that does the same. Loading needs a live Session with a database connection.
Two more terms appear in the error names. A bag is Hibernate's name for an unordered collection that allows duplicates, which is how a java.util.List without an order column is mapped. A transient entity is one created with new that Hibernate has never been told about; it has no row yet.
2Error (a): LazyInitializationException
Here is the classic way to produce it. A service method loads an order inside a transaction, returns it, and a controller or a mapper touches its lines afterwards.
The Session closed when find() returned, so the uninitialized collection has nowhere to load from.
3Fixing (a): Fetch What the Caller Needs Inside the Transaction
The fix is to decide what the caller needs and fetch it while the Session is open. A JOIN FETCH query loads the order and its lines in one statement, an entity graph does the same declaratively on a repository method, and a DTO projection selects only the columns the screen needs, which is often the best choice for read-only views.
Notice that each option makes the fetch plan explicit at the place where we know the use case. The service now promises its caller a fully loaded result, and nothing lazy is left for the controller to trip over.
4Why the Quick Fixes Are Traps
Spring Boot enables spring.jpa.open-in-view by default (and logs a warning about it). Open-in-view keeps the Session open for the whole web request, including view rendering and JSON serialization, so the exception disappears. But now every lazy access in the serializer runs a query, one per order line or per customer, which is the N+1 problem: one query for the list and N more for the items. It also holds a database connection for the whole request, so a slow client or a slow downstream call keeps a pooled connection busy, and under load the pool runs out.
The Hibernate property hibernate.enable_lazy_load_no_trans is worse. It lets a lazy load open a temporary Session and connection on its own, outside any transaction, for each access. Every touch of a lazy association becomes its own short connection and query, with no consistent view of the data between them. Both switches turn a clear error into silent slowness, so we set spring.jpa.open-in-view=false and leave the other property off.
7 messages in order. Play them, or step through them one at a time.
Without open-in-view the second access would fail fast; with it, it quietly becomes N+1.
5Error (b): MultipleBagFetchException
Suppose a Post has List<Comment> comments and List<Tag> tags, and we try to load both in one query.
The reason is the shape of the SQL result. Joining two collections produces a Cartesian product: a post with 20 comments and 5 tags returns 100 rows. With a Set, Hibernate could remove the duplicates, but a bag allows duplicates by definition, so Hibernate cannot tell a real duplicate from a join artefact and refuses.
6Fixing (b): One Collection per Query
The fix is to fetch one collection per query, in the same transaction, so the persistence context merges them onto the same Post instance.
Changing the collections to Set also removes the exception, and it is right when the collection really has set semantics, such as tags. But be careful: with sets the query still returns 100 rows, so for large collections the two-query approach is faster, not just legal.
7Error (c): TransientPropertyValueException
This one appears when an entity we save points to another entity that has never been saved.
Hibernate needs the customer's id to fill the order's foreign key, and the customer has none. It refuses rather than guess whether we meant to save it. If the customer is an independent entity, the fix is to persist it first, or more commonly to use an existing customer loaded with findById or referenced with getReferenceById. If the child truly belongs to the parent, for example order lines created with their order, then cascade = CascadeType.PERSIST (or ALL) on the parent's association is the right fix. We do not add cascade to a @ManyToOne towards a shared entity like Customer just to silence the error, because then saving any order could create customers by accident.
8Follow-up questions
Is open-in-view ever acceptable?
In a small server-rendered app with low traffic it can be tolerable, but it hides the fetch plan of every page. For APIs serving JSON, disabling it and fetching explicitly is the safer default, because problems show up as clear exceptions in development instead of slow queries in production.
Why does Hibernate allow JOIN FETCH of one bag but not two?
With one collection, each result row maps to exactly one child, so the rows can be grouped under their parent without ambiguity. With two, every combination appears, and only a duplicate-free collection type could undo that.
Does getReferenceById avoid TransientPropertyValueException?
Yes, when the referenced row exists. It returns a proxy with the id set, so Hibernate can fill the foreign key without loading the customer. If the id does not exist, the insert fails on the foreign key constraint at flush instead.
9Conclusion
In this answer, we saw three errors and their real causes. LazyInitializationException comes from touching a lazy association after the Session closed, and is fixed with JOIN FETCH, entity graphs or DTO projections rather than open-in-view or enable_lazy_load_no_trans, which cause N+1 queries and long-held connections. MultipleBagFetchException comes from fetching two bags into a Cartesian product and is fixed by fetching one collection per query or using Set where it fits. TransientPropertyValueException comes from referencing an unsaved entity and is fixed by persisting it first or by cascading where ownership is clear.
Open with
For the LazyInitializationException, the cause is that a lazy association, a proxy or uninitialized collection, is accessed after the persistence context has closed, typically when the controller or JSON serializer touches it after the transactional service method returned.
Then, if they want more
The fix is to fetch what the caller needs inside the transaction, with JOIN FETCH, an entity graph, or better a DTO projection for read views.
I would not use open-in-view to hide it, because that keeps the connection for the whole request and turns every lazy access in serialization into extra queries, the N+1 problem, and enable_lazy_load_no_trans is worse because each access opens its own session. MultipleBagFetchException happens when one query join fetches two List collections, which Hibernate maps as bags.
Joining both gives a Cartesian product, say 20 comments times 5 tags is 100 rows, and with a bag Hibernate can't remove duplicates. I'd fetch one collection per query in the same transaction, or use a Set where set semantics are right.
TransientPropertyValueException means an entity references a new, unsaved entity and there's no cascade, so Hibernate has no id for the foreign key. I'd persist the referenced entity first or use getReferenceById, and only add cascade PERSIST when the parent really owns the child.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the cause and the fix for each: (a) LazyInitializationException: could not...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/bbe8fd6696/