• 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

Detached entity doubt

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the spec, If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

Does this mean that there is no way for sure to find out whether a detached object will ever be managed by the persistence context again?? How can we be assured that the detached object will either be managed or an exception is thrown??
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use merge, if it exists it will become managed, if it deoesnt exist IllegalArgumentException will be thrown.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In order to avoid the problem you stated, One needs to merge before doing the update or delete on the entity.
Like, If you want to delete a employee object from the database,
We need to do like,

In the above code, before deleting the employee object, the entity manager make sure that it is synchronize with the database.
Hope this helps.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by krishna bulusu:

<blockquote>code:


<pre name="code" class="core">
entityManager.remove(entityManager.merge(employee));
</pre>


In the above code, before deleting the employee object, the entity manager make sure that it is synchronize with the database.
Hope this helps.
</blockquote>

Merge operation does not synchronize object state with a database, it just makes the instance managed, so it can be used in remove method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic