• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Detached entity doubt

 
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??
 
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.
 
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.
 
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.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic