-->
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.  [ 45 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Interceptor to create history / activity records
PostPosted: Thu Jun 24, 2004 11:37 am 
Newbie

Joined: Thu Oct 09, 2003 4:06 pm
Posts: 16
As luck would have it, right after the blog post about creating history records went up on "In Relation To..." (http://blog.hibernate.org/cgi-bin/blosxom.cgi/2004/06/19/), we've got a requirement to implement something like this. Unfortunately, database triggers were nixed, so we're left looking at Interceptors or Lifecycle callbacks.

I took a look at the History Interceptor article on the wiki (http://www.hibernate.org/195.html), but I'm a little skeptical of the code. I've used it as a template to implement my own interceptor, but my own unit tests show that it doesn't work. I've got a Parent and Child classes, which are in a parent-child relationship, and each of them has a collection of Activity objects (ParentActivity and ChildActivity types, respectively, mapped to different tables). These are mapped as bidirectional parent-child relationships as well.

The problem is that, although the create activities are inserted and can be found by subsequent transactions, the update activities for the parent object are not saved. I think this is because the activities are being added back to the object in postFlush() by the interceptor I copied, so they're not getting flushed, since they'd only be saved based on the cascade from the Parent object to its ParentActivity collection.

I can get this to work, however, if I cheat... I've got my Session in a ThreadLocal, so I can get it even inside the Interceptor, where it's not supposed to be used (at least that was the impression I got). If I explicitly save the new Activity objects I'm creating, then things get inserted successfully. I'm just worried that I'm doing something that's not guaranteed to work, and that I may experience strange errors later.

Anyone have any insight for me? All of this is a little sample app I've been using and adding to to test Hibernate features, so I'm ok to post any / all of it here...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 12:05 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The most important thing about an Interceptor to remember is: you can't use the Session. But you can simply create a new temporary Session _on the same JDBC Connection_:

Session newSession = sessionFactory.openSession(oldSession.connection());

newSession....

newSession.flush();
newSession.close();

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Does the HistoryInterceptor work?
PostPosted: Thu Jun 24, 2004 12:24 pm 
Newbie

Joined: Thu Oct 09, 2003 4:06 pm
Posts: 16
Am I right in thinking that the HistoryInterceptor on the wiki doesn't work? You should probably pull it down, or something, since it's up there and people might try to use it.

Also, is there any way that, in the future, an Interceptor can be allowed to use the Session? It seems like automatically creating some new objects is a pretty common need, and creating a new Session seems like a bit of a hack (not as much of a hack as the HistoryInterceptor, but still).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 12:27 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate3 will have an event-based system that is even more flexible than the Interceptor. There is some design about this on the Wiki and some implementation in CVS.

Actually, I don't think the second Session is a "hack", its a nice trick that is also useful in other situations.

About the Wiki page: I don't pull it down, because I expect people to fix it, if it doesn't work. If I'd repari every page that has been posted, I'd have nothing else to do. :) At least add a comment saying "this and that doesn't work, you might want to try this".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Fri Jun 25, 2004 3:54 pm 
Newbie

Joined: Thu Oct 09, 2003 4:06 pm
Posts: 16
Cool, so this seems to be working. I did leave a comment on that Wiki page, pointing people here. Maybe that will help :-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 01, 2004 3:54 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
Christian,

You advised us to use:
Code:
Session newSession = sessionFactory.openSession(oldSession.connection());
newSession.flush();
newSession.close();


However, from my experiments, if I have to perform the code segments above inside an interceptor, I cannot close the newSession. Because I might still need to work on the oldSession.

Regards,

Alistair


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 01, 2004 6:27 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 4:43 pm 
Regular
Regular

Joined: Fri Aug 29, 2003 12:48 pm
Posts: 63
I think he's worried that newSession.close() will close the JDBC connection provided by the old session. The session API seems to indicate that it won't actually close the connection if it was explicitly provided by the user, but it's a little ambiguous. Can you confirm?

Why do you flush before closing, anyway? Isn't that redundant?


Top
 Profile  
 
 Post subject: Where is it?
PostPosted: Wed Sep 15, 2004 8:14 pm 
Newbie

Joined: Wed Sep 15, 2004 8:11 pm
Posts: 5
Am I missing something or did you post the code for this anywhere? It would be nice to see...


Top
 Profile  
 
 Post subject: Re: Where is it?
PostPosted: Thu Sep 16, 2004 3:18 am 
Newbie

Joined: Thu Oct 09, 2003 4:06 pm
Posts: 16
jmattson wrote:
Am I missing something or did you post the code for this anywhere? It would be nice to see...


What code are you looking for? I just used the new Session like Christian showed and used the new Session to save the new objects I was creating and it worked.


Top
 Profile  
 
 Post subject: previousState [] is null in onFlushDirty
PostPosted: Thu Sep 23, 2004 9:34 am 
Newbie

Joined: Tue Jun 22, 2004 11:35 am
Posts: 10
hi
i have created an interceptor and in the onFlushDirty method i'm trying to compare the previousState against the currentState. i assumed that when updating a row the previousState[] would be populated but it's not, it's null. do i need to specify the fact that i would like the previousState[] populated for updates and if so how?? any help on this would be greatly appreciated
thanks in advance
howard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 10:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you use update(), Hibernate has no snapshot. That is the nature of update()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 12:14 pm 
Newbie

Joined: Tue Jun 22, 2004 11:35 am
Posts: 10
so what shoud i use to make sure that the previousState[] is available because the java doc didn't mention that fact for update()
thanks for your help


Top
 Profile  
 
 Post subject: Audit Log / Hibernate dirty props array
PostPosted: Thu Oct 14, 2004 3:32 pm 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
After implementing an audit log using the interceptor, and digging though the Hibernate code because of an issue relating to a CGLib optimized class not being equal() to the class it as based on, I noticed that when Hibernate calls the Interceptor, it already has figured out the dirty properties, and it has their indexes stored in an Array - why dont we pass this array through as well. That would save everyone in the Audit log use case from having to re-eval the oldValues against the newValues, thereby wasting time.

Nick


Top
 Profile  
 
 Post subject: How do I avoid a JNE with oldValue[] or previousState[]
PostPosted: Mon Oct 18, 2004 2:03 pm 
Newbie

Joined: Mon Oct 18, 2004 1:42 pm
Posts: 10
Location: Ottawa, Canada
I am trying to adapt the HistoryInterceptor (http://www.hibernate.org/195.html) but I am running into a JNE which seems to be related to Hibernate having no snapshot.

I am getting this error even though I am using save() and not update().

Thanks.

Josh.



gavin wrote:
If you use update(), Hibernate has no snapshot. That is the nature of update()


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 45 posts ]  Go to page 1, 2, 3  Next

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.