users@jpa-spec.java.net

[jpa-spec users] [jsr338-experts] Re: changes for 7.6.2 Container-managed Transaction-scoped Persistence Context

From: Linda DeMichiel <linda.demichiel_at_oracle.com>
Date: Wed, 30 Nov 2011 10:53:48 -0800

On 11/30/2011 7:26 AM, Emmanuel Bernard wrote:
>
> On 30 nov. 2011, at 00:48, Linda DeMichiel wrote:
>
>>>> Just to be very clear.... What are you referring to by "container level
>>>> method
>>>> invocation call"? The call to the entity manager proxy?
>>>
>>> Not the call to the entity manager proxy. I mean the code that is calling the entity manager proxy (e.g. a session bean
>>> calling the entity manager proxy.)
>>>
>>> Perhaps a clearer proposal could be:
>>>
>>> "
>>> If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database will
>>> immediately become detached at the end of the container level method invocation call (e.g. when the session bean method
>>> ends).
>>> "
>>
>> Ah, thanks. That is not the intended semantics. Rather it is that the
>> persistence context is created to service the method call that is made
>> on the entity manager only. It doesn't have the duration of a business
>> method invocation.
>
> Is it really?
>

Yes. See also section 3.8.7, which is quite explicit with regard to queries.
BTW, these decisions date back to JPA 1.0 (aka EJB 3.0).

> I have to admit I am amongst people that read this as bound to the duration of the business method call. The main problem with this approach is that many em.find() calls are essentially useless as they would not load (lazy) associations.
> Upon detachment, the provider then raises a NotLoadedException of some sort. The only way to work around that is to write a query with the specific fetch pattern that ought to be used.
> If entities are detached after the business call, the user can navigate and use PersistenceUtil contracts to load the appropriate part of the graph before ending the method.
>
> Note that for a PC that detaches entities upon EM method calls, Hibernate has a specific API with specialized contracts to tailor that kind of use case. I don't think our existing EM API is necessarily well suited for that per se.
>
> Emmanuel