I agree, in general the application *should* not need to know whether an entity is dirty. The JPA provider tracks that for you and issues the appropriate insert / update to the database. There are some edge cases where it is desireable to know, but this doesn't sound like one of them (to me at least)..
Applications may be interested in knowing whether an entity instance is contained by a given EM (think of the EM as a collection of rows in memory) and whether that EM is actively participating in a transaction.. The former can bet determined by calling em.contains(myEntity), the latter is tricker with JEE involved, but em.getTransaction().isActive() will work for JSE.. Roger, it sounds like you're dealing with detached instances of the entities (the ones in the collection) - so calling em.merge() would re-attach them to the persistence context (EM). Hope this helps, -mike On Thu, Jun 25, 2009 at 11:38 AM, Luis Fernando Planella Gonzalez < [email protected]> wrote: > Why do you need to know if the entities are dirty or not? > JPA automatically persists dirty entities. > So: > MyEntity e1 = load(MyEntity.class, id); > e1.setProperty("new value"); > //This will be saved when the EntityManager is flushed > > So, for new instances, call persist(). For disconnected instances, call > merge(). For managed instances, just update them. > > Luis Fernando Planella Gonzalez > > > Em Quinta-feira 25 Junho 2009, às 14:07:57, Roger escreveu: > > On Thursday 25 June 2009 16:27:20 Daryl Stultz wrote: > > > On Wed, Jun 24, 2009 at 4:22 PM, Roger <[email protected]> > wrote: > > > > is there a way > > > > through the "enhanced" version to detect which entities have been > changed > > > > so I > > > > don't call persist() for them, > > > > > > Well, since no one else has replied, I might be able to help. Try this: > > > OpenJPAEntityManager openJpaEm = (OpenJPAEntityManager) em; > > > assertTrue(openJpaEm.isDirty(entity)); > > > > > > > Is it ok to do that? Shouldn't I be working through the generic > Persistence > > API rather than "tying" myself to the specific implementation? > > > > > > > > > > or do I persist() them all and trust openJpa to > > > > only physically persist entities that have changed? > > > > > > First I assume you mean merge() rather than persist() as persist() is > > > something you do with new entities. > > > > > > > I am more of a newbie than I thought then. I thought merge() was > something > > that you did when you had a "disconnected entity. Are there any good > tutorials > > online anywhere for beginners - I find the openjpa manual a bit turgid > and > > assumes that I know more about things than I actually do > > > > Regards > > >
