wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityManager" Watch "EntityManager" New topic
Author

EntityManager

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
EJB 3 in Action page 304,

em.refresh(em.merge(item)); // to undo changes in entity (refresh from db)

em.remove(em.merge(item)); // to remove entity from db

Doubt :
em.merge(item) updates the db, then why it is used with refresh / remove method....

Promod kumar
Ranch Hand

Joined: Jun 26, 2006
Posts: 90
Both refresh and remove require an attached entity as the parameter, call to merge makes the entity attached. If the entity is not managed, an IllegalArgumentException will be thrown.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Deepika,

that's a good observation. In fact, I think there is no guarantee that

em.refresh(em.merge(item)); // to undo changes in entity (refresh from db)

works as it was desired in EJB 3 in Action: The persistence provider my choose to synchronize the persistence context with the database right after em.merge(item) was performed (justification by spec see below) - though it's unlikely. Then em.refresh(...) would read the just updated data from database. It's safer to use

em.refresh(em.find(Item.class, item.id));

to undo the changes.

JPA spec 3.2.3 (Synchronization to the Database)
The state of persistent entities is synchronized at transaction commit. [...]
The persistence provider runtime is permitted to perform synchronization to the database at other times as well when a transaction is active.


SCJP 5 (98%) - SCBCD 5 (98%)
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
thanks Promod & Ralph for your reply....

logicaly....
em.refresh(em.find(Item.class, item.id));
should be only choice to refresh (update entity from db)

thanks for your help...

Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

Ralph:
i think you are right, refresh usually UNDO changes done in the
code overwriting it with DB state..

but there's one thing i don't understand.
if you want to update the DB and get a managed entity
why not use

instead of




SCJP 5, SCWCD 5, SCBCD 5
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Jonathan,

who says you should use

em.refresh(em.merge(item)) ; // (1)

instead of

item=em.merge(item) ; // (2)

in order to update the db and have returned a managed entity ? In this situation I would usually prefer (2).
But that's not the situation descriped by Deepika.
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170


oh, sorry, a misunderstanding...
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I think this is so lame that the EM needs to call merge before refresh or remove.
Why refresh and remove cannot handle that? Any opinions?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
Ralph Jaus wrote:Hi Deepika,

that's a good observation. In fact, I think there is no guarantee that

em.refresh(em.merge(item)); // to undo changes in entity (refresh from db)

works as it was desired in EJB 3 in Action: The persistence provider my choose to synchronize the persistence context with the database right after em.merge(item) was performed (justification by spec see below) - though it's unlikely. Then em.refresh(...) would read the just updated data from database. It's safer to use

em.refresh(em.find(Item.class, item.id));

to undo the changes.

JPA spec 3.2.3 (Synchronization to the Database)
The state of persistent entities is synchronized at transaction commit. [...]
The persistence provider runtime is permitted to perform synchronization to the database at other times as well when a transaction is active.



em.refresh(em.find(Item.class, item.id));

another thought to above statement,
-refresh is to update entity
-find is used to attach entity (to get it to manage state)

but this also solves the purpose
item = em.find(Item.class, item.id);

we do not need to run refresh() in this case, and can save a db call.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Please note that

1. em.refresh(em.find(Item.class, item.id));
2. item = em.find(Item.class, item.id);

aren't equivalent in general: If the item is already managed, 2. will return the state from persistence context - not from db, while 1. will override (refresh) the state in persistence context with db state.
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268


Thanks a lot for replying....
 
 
subject: EntityManager
 
Similar Threads
Query on Detached and Removed Entities
when the CMR collection gets refreshed?
Doubts about refresh + merge function in JPA
Using other JDBC driver version within JDeveloper
Entitymanager.refresh doubt