aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Detached entity doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Detached entity doubt" Watch "Detached entity doubt" New topic
Author

Detached entity doubt

Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
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

Joined: Jul 06, 2008
Posts: 85
use merge, if it exists it will become managed, if it deoesnt exist IllegalArgumentException will be thrown.


SCDJWS 5.0, SCBCD 5.0, SCWCD 5.0, SCJP 5.0
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
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.


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Detached entity doubt