Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EntityManager.refresh() works only on managed entities.

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't EntityManager.refresh() work on detatched entities? Any reason behind this ?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic