• 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.refresh() works only on managed entities.

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't EntityManager.refresh() work on detatched entities? Any reason behind this ?
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose there is no great reason, other than that is what the JPA spec requires.

Refresh, refreshes the managed instance of the object, as that it the instance you should be using, and the only instance which you can update. Refreshing a detached instance may have identity issues with relationships and other such complexities.

If you are using EclipseLink, there is a ReadObjectQuery option shouldLoadResultIntoSelectionObject that can be used to refresh into a detached object. You can also enable refreshing on any query, you don't need a handle on the object to refresh it.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Sutherland wrote: Refreshing a detached instance may have identity issues with relationships and other such complexities.



Well i personally don;t see any complexity in refreshing entity graph using PK. As the PK is intact in detached instances, we can always load(refresh) the entity graph.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover at P-298, the diagram shows refresh() method working on detatched entity... Is that an errata ?
P-331 says : "We use the method to undo changes made by the ItemManager client and return fresh entity data from the database."
but when we call merge() (before refresh()), the changes made by the client will get reflected in the database.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[P-298 Of EJB3 In Action Book] States: In the diagram, it shows that using a refresh method a detatched entity can become managed..
[P-332Of EJB3 In Action Book] States: The refresh method only works on managed entities...
Aren;t the above 2 statements contradictory? Moreover Statement1 is incorrect...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic