-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Determining before-hand if an object has been updated
PostPosted: Wed Sep 17, 2003 2:45 am 
Newbie

Joined: Fri Aug 29, 2003 10:14 pm
Posts: 3
Does Hibernate provide an API call for determining if a persistable object is dirty?

I can always check for the id field being null for the new objects that are created before being written on the database, but I'd like to know if setXXX has been called for any field on objects loaded from the database.

I have a list of objects, and I'd like to iterate through those, and then for the ones that are new, called save() on those, and for the ones that are updated, I want to set up an audit record, and then call saveOrUpdate() on those.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 9:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Yes, its called Interceptor. The method you would be interested in is named onFlushDirty(). onFlushDirty gets passed three parallel arrays which indicate property state changes. Note however that there are some peculiarities with Interceptor usage for this purpose. The most frustrating being that changes to collection-based property of an entity will not cause the onFlushDirty method to be called (if some other properties also changed, it is called). But this may or may not be a big issue for you.

Check out the docs and see if this fits your needs.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2003 6:30 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
steve wrote:
The most frustrating being that changes to collection-based property of an entity will not cause the onFlushDirty method to be called (if some other properties also changed, it is called).


What do you mean about this? I wrote a simple example with two classe a and b. a contains a collection of b.

I tried
Code:
load_a = load(a, key)
load_b = load(b, key)
load_a.setB(load_b)
save(load_a)
flush()

and the flush() caused an onFlushDirty()!?!

Could you give a more detailed example, when changing a collection does not cause an onFlushDirty()?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2003 10:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Not sure how "load_a.setB(load_b)" constitutes changing a collection, but you know your app better than me. Also, I am assuming that save is not calling Session.save()

Consider:
Code:
load_a = load(a, key)
load_b = load(b, key)
load_a.getB().add(load_b)
save(load_a)
flush()


where a has a collection of references to B. flush() will not cause an onFlushDirty() call to the interceptor here...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2003 12:29 pm 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
Uups, sorry!!!! I've tested with a many-to-one association, not with a collection. Have to be more careful the next time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 6:02 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
Hm, first I thought the problem Steve described above doesn't occur. Now I'm running into that behaviour and have to find a workaround :-(

Is there a simple workaround, that flush() causes Interceptor.onFlushDirty() to be called, without changing a non collection property to a "wrong" value?

Or will this behaviour be fixed in a later Hibernate version?

(What I want is, that a flush() causes a call of Interceptor.onFlushDirty(), even if only a collection of an object has changed).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 8:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
There was a long discussion about this prior to 2.1 release (back on the old forums). As far as I know it was dropped for the time being.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 3:53 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
I read the thread at the sourceforge forum, now. Mybe Gavin could tell us, what's the actual planning about this topic?

Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.