aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes find and getReference methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "find and getReference methods " Watch "find and getReference methods " New topic
Author

find and getReference methods

Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
"The find and getReference methods are NOT required to be invoked within a transaction context. If an entity manager with transaction-scoped persistence context is in use, the resulting entities will be DETACHED; if an entity manager with an extended persistence context is used, they will be MANAGED."

This is the statement from MZ notes and have question on this..

If the resulting entity from find/getReference method is DETACHED when the entity manager with transaction-scoped persistence context is in use, then how can we operate/update these entities in that context when they are not in managed state?, can somebody explain, am I missing something here ?


Preparing SCEA..<br />SCBCD 5.0<br />SCWCD 1.4<br />SCJP 1.4
bernard savary
Ranch Hand

Joined: Jan 09, 2008
Posts: 91
Hi ,

Entity manager operations like persist , merge etc when called through a transaction scoped entity manager requires a transaction context to be present or else an exception will be thrown . So if you want to update the entity using a transaction scoped EM , then you will have to do it inside a transactional context .
Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
My question entirely diffrent from your answer, I understand that Entity has to be in TC to invoke persist etc...

The resulting entity is DETACHED after 'find' method is invoked, then how would call persist on it ?
Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
OK, Here is what it says in the code, I guess the two statements are bit confusing..

Rajendra Vittal
Greenhorn

Joined: Apr 25, 2005
Posts: 5
Hi Shonak,

When there is no transaction (such as methods annotated with TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)) there won't be any PersistenceContext associated with the transaction, so every time find method returns a new entity instance for the same primary key. If you invoke em.find() method in a method where transaction is active then always find returns same entity instance for the same primary key.

I understood one thing with EJB3.0-"Too much simplicity(easiness) also confuses the users". Here with transaction-scoped PersistnaceContext, container takes care of everything so user is unaware of what is happening.

I suggest you to read Chapter5 Pro EJB3 by Mike Keith and Merrick Schincariol. They have given best explanation which you don't find in other books.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi,
Please look the following code:

My Client Code:

According the MF notes, the find method returns a detached object. Now, I am trying to delete a detached object. so, The system must throw an exception saying that the object is detached.
If I comment out the find method, I am getting the following exception:
Exception in thread "main" javax.ejb.EJBException: java.lang.IllegalArgumentException: Removing a detached instance bala.persistent.Employee#11
Imust get the same exception if I uncomment the find exception.
Please anybody explain this.


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

According the MF notes, the find method returns a detached object. Now, I am trying to delete a detached object. so, The system must throw an exception saying that the object is detached.


The find() method returns a detached object if called from no transaction context. In your example, you are calling find() from inside a transaction:



SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

If the resulting entity from find/getReference method is DETACHED when the entity manager with transaction-scoped persistence context is in use, then how can we operate/update these entities in that context when they are not in managed state?

Merge or refresh them. The way you would do with any detached entity.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find and getReference methods