users@glassfish.java.net

sth doubt about detached entity

From: Wu Jie <wujie_at_cn.fujitsu.com>
Date: Tue, 26 Aug 2008 18:34:48 +0800

Hi Peter
Hi GF) Dev Teams,
Hi Everyone,

I did some test against JPA(TopLink Essentials), one phenomenon happened
with detached entity.

[1] Phenomenon Description
First I transformed an entity instance which has field or property marked
fetch=LAZY to detached entity instance by invoking em.close()(the persistent
field or property was never accessed by the application before). and then
I accessed the field or property which marked fetch=LAZY of the detached
entity instance. and the returned value was not null. and SQL statement
for the request(access the field) was printed in CLI console.
[note]
・the test env is SE
・the relationship of the persistent field marked fetch=LAZY is One:One

[2] Description of Detached Entity in JPA Specification
Detached entity instances continue to live outside of the persistence context
in which they were persistedor retrieved, and their state is no longer
guaranteed to be synchronized with the database state.
The application may access the available state of available detached entity
instances after the persistence context ends. The available state includes:
• Any persistent field or property not marked fetch=LAZY
• Any persistent field or property that was accessed by the application

If the persistent field or property is an association, the available state
of an associated instance may only be safely accessed if the associated
instance is available. The available instances include:
• Any entity instance retrieved using find().
• Any entity instances retrieved using a query or explicitly requested in
   a FETCH JOIN clause.
• Any entity instance for which an instance variable holding non-primary-key
   persistent state was accessed by the application.
• Any entity instance that may be reached from another available instance
   by navigating associations marked fetch=EAGER.

[3] My Doubt
the phenomenon seems to go against the JPA Specification. Could you please
give some suggestion or some matierial. and any help is appreciated.

Best Regard.
-Wu Jie