I have a JAX-RS resource class. It invokes a stateless EJB which loads an entity (let's call it Parent
).
Parent
has a @ManyToOne
relationship with another entity (let's call it Child
) and is configured as having a fetch type of LAZY
.
No additional transactional annotations appear on anything, so the stateless EJB has the default behavior of starting a transaction when necessary, and committing the transaction once its business method has completed.
In a debugger, I observe that Parent
as returned by the EJB has null
as the value of Child
, just as I'd suspect. Lazy loading therefore is working fine.
My resource class then builds a Response
and hands off control to the JAX-RS innards. Again, no transaction is open.
After serialization, the JSON (in my case) that shows up contains all the fields of Child
. Something in the JAX-RS innards is somehow "inflating" that Child
reference outside of the transaction.
I observe in a debugger that indeed at some point well after JAX-RS has taken over control that the Child
reference has become non-null
.
I thought that either Child
should have stayed null
or that an exception of some kind should have been thrown to indicate that someone tried to access a lazy-loaded field. Clearly I misunderstood something.
Under what circumstances may a detached JPA entity have its lazily-loaded relationships "inflated" outside of a transaction?
If it matters, I'm using Jersey 2.10.4 by way of Glassfish 4.1 and EclipseLink 2.5.2 (again, by way of Glassfish 4.1).