5 Replies Latest reply: Feb 23, 2007 10:07 AM by DougClarke RSS

    Detached entity with TopLink essentials

    509441
      I am testing features of detached entities with a standalone application, and particularly with lazy loading association.

      It seems that it is very difficult to "completely" detache an entity of a standalone application with TopLink. Even if I close the entity manager or the entity manager factory after having detached the entity, the detached entity finds how to load the entities of the lazy loading association from the database, when it needs it (without any merge call). Apart serializing and deserializing, do you know how to detach an entity without a possibility of reading the missing information, in a standalone application?

      Can someone tell me where I can find details about detached entities in TopLink and particularly what information are kept by a detached entity to succeed in getting missing information to the database without a call to the merge method? if possible without having to read the source code...

      Thanks in advance for your answers.

      Richard
        • 1. Re: Detached entity with TopLink essentials
          509441
          I have got no answers to my previous post, so I will try to make my questions more precise. There is certainly something I have not well understood.

          In a standalone application (without any application server), with toplink essentials,

          1. Is an entity (an employee) queried outside of a transaction is always detached? (Query query = em.createQuery("select e from Employee as e");)

          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?

          Richard
          • 2. Re: Detached entity with TopLink essentials
            532169
            I have the same question,anyone can help?
            • 3. Re: Detached entity with TopLink essentials
              DougClarke
              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
              • 4. Re: Detached entity with TopLink essentials
                562860
                I have stepped upon the same feature, and as far as I can see it works the same way in container as in a stand-alone Java SE 5 application. It also seems to work the same way with entities fetched from a tx-read. Is this correct?

                Is there any documentation to be found somewhere on exactly how this feature works?

                I would also like to know if there is a way to turn it off?

                Regards Sofia
                • 5. Re: Detached entity with TopLink essentials
                  DougClarke
                  Sofia,

                  If you would like TopLink Essentials to not process lazy relationships after the EM has closed I would recommend filing an enhancement request in GlassFish.

                  Doug