Thread: Detached entity with TopLink essentials


Permlink Replies: 5 - Pages: 1 - Last Post: Feb 23, 2007 8:07 AM Last Post By: Doug Clarke
user506438

Posts: 27
Registered: 04/27/06
Detached entity with TopLink essentials
Posted: Jul 16, 2006 3:15 AM
Click to report abuse...   Click to reply to this thread Reply
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
user506438

Posts: 27
Registered: 04/27/06
Re: Detached entity with TopLink essentials
Posted: Jul 23, 2006 1:25 AM   in response to: user506438 in response to: user506438
Click to report abuse...   Click to reply to this thread Reply
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
user529166

Posts: 1
Registered: 09/06/06
Re: Detached entity with TopLink essentials
Posted: Sep 6, 2006 3:44 AM   in response to: user506438 in response to: user506438
Click to report abuse...   Click to reply to this thread Reply
I have the same question,anyone can help?
Doug Clarke

Posts: 1,933
Registered: 06/25/02
Re: Detached entity with TopLink essentials
Posted: Sep 6, 2006 4:28 AM   in response to: user506438 in response to: user506438
Click to report abuse...   Click to reply to this thread Reply
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
user559857

Posts: 2
Registered: 02/20/07
Re: Detached entity with TopLink essentials
Posted: Feb 20, 2007 5:00 AM   in response to: Doug Clarke in response to: Doug Clarke
Click to report abuse...   Click to reply to this thread Reply
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
Doug Clarke

Posts: 1,933
Registered: 06/25/02
Re: Detached entity with TopLink essentials
Posted: Feb 23, 2007 8:07 AM   in response to: user559857 in response to: user559857
Click to report abuse...   Click to reply to this thread Reply
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
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums