This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
"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 ?
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 .
Joined: Apr 07, 2005
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 ?
Joined: Apr 07, 2005
OK, Here is what it says in the code, I guess the two statements are bit confusing..
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.
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.
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.