• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EntityManager

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

oh, sorry, a misunderstanding...
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks a lot for replying....
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic