persistence@glassfish.java.net

RE: Determining if entity has been removed

From: Markus KARG <markus.karg_at_gmx.net>
Date: Tue, 9 Dec 2008 17:58:12 +0100

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

If it is the same transaction then you know very well what you did. Why do
you need JPA to tell you what your own code did?

If it is a differen transaction then you will not see the change anyways
unless the transaction is finished, depending on the transaction isolation
level. So JPA cannot know that the object is removed meanwhile.

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

You write "a NEW entity". So it will be a persist (= INSERT) always. Or do
you mean a DETACHED entity?

> 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 think you do not mean "whether it was deleted" but rather "whether it does
not exist currently", what can be easily solved e. g. by em.find().

> I am curious why there is no method(s) to determine state of en entity
> within an EntityManager in the API.

Uh... there ARE several such methods... Maybe we just do not understand what
you really want to do...?

Regards
Markus

>
> Thanks.
>
> Markus KARG wrote:
> > AFAIK JPA does not contain a means of detection wheter an entity has
> been
> > removed, but only whether an instance of an entity is present or not.
> Also I
> > do not see a sense in such a detection. What shall it be good for?
> >
> >
> >> -----Original Message-----
> >> From: Farrukh Najmi [mailto:farrukh_at_wellfleetsoftware.com]
> >> Sent: Sonntag, 7. Dezember 2008 23:14
> >> To: persistence_at_glassfish.dev.java.net
> >> Subject: Determining if entity has been removed
> >>
> >> Dear colleagues,
> >>
> >> How can I determine whether an entity has been removed within the
> >> EntityManager?
> >> More generally is there any way in the JPA API to determine status
> of
> >> an
> >> entity within
> >> the EntityManager? It does not appear to be possible. If not
> possible,
> >> is this something useful to
> >> consider for future version of API?
> >>
> >> Thanks.
> >>
> >> --
> >> Regards,
> >> Farrukh Najmi
> >>
> >> Web: http://www.wellfleetsoftware.com
> >>
> >
> >
>
>
> --
> Regards,
> Farrukh Najmi
>
> Web: http://www.wellfleetsoftware.com