• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Detached entity doubt

 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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??
 
Vinay Nath
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use merge, if it exists it will become managed, if it deoesnt exist IllegalArgumentException will be thrown.
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • 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.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic