persistence@glassfish.java.net

Re: Determining if entity has been removed

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Tue, 9 Dec 2008 15:52:25 +0100

On Tue, Dec 09, 2008 at 09:32:46AM -0500, Farrukh Najmi wrote:
> Without going into gory details of my application here is my use case:
>
> * I have an existing entity in db
> * Within a transaction the following occurs:
> o The existing entity gets read by transaction
> o The existing entity *MAY* get removed by transaction (or not)
> o A new entity is inserted with the same id. The code to do
> that needs to know whether to use a merge or insert. One way
> to decide is to know if the netity was deleted or not. If it
> was deleted do an insert otherwise do a merge.
>
> I am curious why there is no method(s) to determine state of en entity
> within an EntityManager in the API.

Well, how would an entityManager know if an Entity was deleted? Would you expect
it to keep a history of all deleted Entities?

To determine whether an Entity already exists you may use contains(). One way to
determine if a row with a certain id already exists is to use the find method
with the given id. Note that this may lead to a database round trip.


HTH, Wouter van Reeven

-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"