persistence@glassfish.java.net

Re: Determining if entity has been removed

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Tue, 09 Dec 2008 10:17:37 -0500

Wouter van Reeven wrote:
> 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?
>

I expect it keep track of all entities deleted within the transaction
prior to the transaction being committed.

> 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.
>

I already know the entity exists in db. What I need to know is whether
the active transaction has removed it or not so I can decide whther to
do a em.insert() or em.merge() operation. At present I do an em.merge()
and if I get an exception I try em.insert. This is klunky at best.


-- 
Regards,
Farrukh Najmi
Web: http://www.wellfleetsoftware.com