You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by David Goodenough <da...@btconnect.com> on 2008/04/11 23:09:37 UTC

How to detect whether there are any dirty objects?

Is there a way to detect whether there are any modified/dirty objects
that need to be written to the datastore?  I do not need to know which 
objects are dirty, just whether there are any.

I am trying to add the kind of functionality that puts up a question
when someone tries to terminate the program without saving to 
point out that they need to save.

Thanks,

David

Re: How to detect whether there are any dirty objects?

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  You need to downcast standard EntityManager to OpenJPA specific entity
manager which provides many extended utilities including access to dirty
objects in the current transaction.
 
  EntityManager em = ...;
  OpenJPAEntityManager oem = OpenJPAPersistence.cast(em); 
  Collection dirtyObjects = oem.getDirtyObjects();



David Goodenough-3 wrote:
> 
> Is there a way to detect whether there are any modified/dirty objects
> that need to be written to the datastore?  I do not need to know which 
> objects are dirty, just whether there are any.
> 


-- 
View this message in context: http://www.nabble.com/How-to-detect-whether-there-are-any-dirty-objects--tp16641615p16693472.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: How to detect whether there are any dirty objects?

Posted by Georgi Naplatanov <go...@oles.biz>.
Hello, David,

EntityTransaction.isActive() really don't do this. It's my mistake.

Because i'm interesting for this issue too, i make the following test,
but without success:

EntityManager em = emf.create EntityManager() ;
em.getTransaction.begin() ;
Query q = em.createQuery("DELETE FROM Myobject x") ;
q.executeUpdate() ;

OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);

boolean isDirty = !(kem.getUpdatedClasses().isEmpty() &&
kem.getPersistedClasses().isEmpty() && kem.getRemovedClasses().isEmpty() ) ;

em.getTransaction().rollback() ;

But isDirty is always false. May be these methods do not work with queries.

Best regards
Georgi

David Goodenough wrote:
> On Friday 11 April 2008, Georgi Naplatanov wrote:
>> Hello, David.
>>
>> In your case EntityTransaction.isActive() should do the job.
>>
>> Best regards
>> Georgi
>>
>> David Goodenough wrote:
>>> Is there a way to detect whether there are any modified/dirty objects
>>> that need to be written to the datastore?  I do not need to know which
>>> objects are dirty, just whether there are any.
>>>
>>> I am trying to add the kind of functionality that puts up a question
>>> when someone tries to terminate the program without saving to
>>> point out that they need to save.
>>>
>>> Thanks,
>>>
>>> David
> 
> The reference manual says that isActive tells me whether begin has been
> called more recently than commit or rollback (section 9.2).  Are you saying
> that if begin has been called but nothing has been modified that it will also
> return true.  Also if the last thing that was called was commit or rollback
> and begin has not yet been called will it be true if something has been 
> modified?
> 
> David


Re: How to detect whether there are any dirty objects?

Posted by David Goodenough <da...@btconnect.com>.
On Friday 11 April 2008, Georgi Naplatanov wrote:
> Hello, David.
>
> In your case EntityTransaction.isActive() should do the job.
>
> Best regards
> Georgi
>
> David Goodenough wrote:
> > Is there a way to detect whether there are any modified/dirty objects
> > that need to be written to the datastore?  I do not need to know which
> > objects are dirty, just whether there are any.
> >
> > I am trying to add the kind of functionality that puts up a question
> > when someone tries to terminate the program without saving to
> > point out that they need to save.
> >
> > Thanks,
> >
> > David

The reference manual says that isActive tells me whether begin has been
called more recently than commit or rollback (section 9.2).  Are you saying
that if begin has been called but nothing has been modified that it will also
return true.  Also if the last thing that was called was commit or rollback
and begin has not yet been called will it be true if something has been 
modified?

David

Re: How to detect whether there are any dirty objects?

Posted by Georgi Naplatanov <go...@oles.biz>.
Hello, David.

In your case EntityTransaction.isActive() should do the job.

Best regards
Georgi

David Goodenough wrote:
> Is there a way to detect whether there are any modified/dirty objects
> that need to be written to the datastore?  I do not need to know which 
> objects are dirty, just whether there are any.
> 
> I am trying to add the kind of functionality that puts up a question
> when someone tries to terminate the program without saving to 
> point out that they need to save.
> 
> Thanks,
> 
> David