Hi Michael,
thank you for your response, it makes sense for me. My misunderstanding came from wrong understanding of persist method. I wrongly assumed, that the persist method provides functionality to store selected managed objects.

I see now, that all managed objects are updated in DB during commit. Is there any possibility to choose just objects I want to update (except detaching object I don't want to update)?

When I call remove on a managed (and changed) object, why does UPDATE get called before DELETE?


Thanks Bardolf

Michael Dick wrote:
Hi Bardolf,

In the example above you're using an application managed EntityManager.
Application managed EMs are considered extended persistence contexts by
default (ie the lifecycle of the persistence context can cross
transactions).

In this environment when you call EntityManager.find() a managed entity will
be returned. Since the entity is managed any changes you make to it will be
committed when you commit the transaction.

If you don't want the entity to be managed you can call EntityManager.clear()
which detaches all entities or you can call
OpenJPAEntityManager.detach(myEntity)
to detach a single instance. The code might look something like this :

EntityManager em =
javax.persistence.Persistence
.createEntityManagerFactory("JavaApplication3PU").createEntityManager();
Address ad = em.find(Address.class,1);
ad.setMyProperty(xxx);

em.clear();  // detach all managed entities

// Detach a single entity
// OpenJPAPersistence.cast(em).detach(ad);

em.getTransaction().begin();

// if you decide you want the changes to be made after all uncomment this
line
// ad = em.merge(ad);

em.getTransaction().commit();

I'm writing this from memory so this might not quite match up.

Hope this helps,
-Mike

On Fri, Mar 21, 2008 at 8:36 PM, bardolf <[EMAIL PROTECTED]> wrote:

Hi all,
I have following question. Why JPA call updates on all changed entities
during commit even though persist wasn't called.

[snip]
EntityManager em =
javax.persistence.Persistence.createEntityManagerFactory
("JavaApplication3PU").createEntityManager();
Address ad = em.find(Address.class,1);
ad.setMyProperty(xxx);
em.getTransaction().begin();
em.getTransaction().commit();
[/snip]

And even though I didn't call persist, changes are updated in database.
WHY???

Thanks B.




Reply via email to