Greetings, I have a doubt: what happens if a session bean returns a lazily initialized persistent entity? Are the lazy collections readable?
I have three cases: 1. A stateless session bean invoked by a servlet 2. A stateful session bean invoked by a servlet 3. An inner stateless session bean (local interface) invoked by a session bean 4. An inner stateful session bean (local interface) invoked by a session bean
I have tried the four cases: 1. RuntimeException: no session 2. RuntimeException: no session 3. Ok 4. Ok
Well, I collected the following conclusions, respectively: 1, 2. A remote method returns Objects by copy. Moreover, if the persistence context is transaction type, the returned object is returned as detached 3, 4. Inner session bean share the same injected persistence context. Moreover, local interfaces return objects by reference.
Is all right?
But, if an inner session bean (cases 3, 4) has a transaction persistence context injected, it should always return disconnected objects, but it does not seem to be true: in fact, in cases 3, 4 the object returned by the inner session bean has readable lazy collections.
I think there is no relation of lazy relations and basic types with the type of session beans. The lazy fetch objects are available when you use the find operation or JOIN FETCH on these entities (parant entities form lazy object are accessible) or you have referring to non-primary key fields of the lazy object.
More ever I specification explains that the fetchType.LAZY is only hint to the Persistence provider it is not necessary that lazy objects are fetched when they used. I persistence provider can fetch these objects eagerly if they required for the integrity of the application.