1. Is an entity (an employee) queried outside of a
transaction is always detached? (Query query =
em.createQuery("select e from Employee as e");)
Yes, as per the specification's requirements.
2. Then, after having got an entity (employee),
even if I close the entity manager, I can get
the name of the projects where this employee
participates (by calling
employee.getParticipations().getProject().getName()
in a loop). However, the association between
employees and projects is represented by an
association class Participation and the property
getParticipations() in Employee represents a
OneToMany association, so it is lazy fetched, isn't
it? I have not read any Participation before closing
the entity manager. Why haven't I got an exception
because of the lazy fetching?
This is a special feature of TopLink's implementation where the detached instances created from non-tx reads still have access in their proxies to retrieve additional dettached instances. If the object was detached through serialization this would not be possible.
Doug