File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find and getReference methods

 
Ashwin Kumar
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ?
 
bernard savary
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Here is what it says in the code, I guess the two statements are bit confusing..

 
Rajendra Vittal
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic