-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using Interceptor for recording of changes history?
PostPosted: Tue Apr 13, 2004 11:00 am 
Newbie

Joined: Tue Apr 13, 2004 6:41 am
Posts: 19
Hi,

What I would like to do is to create backlog of changes by hooking Interceptor onFlushDirty method and saving changes in same transaction as flush happens.

Is it possible to save History instance (newly created intsnce which has list of changes recorded) along with changed object using same flush/commit within onFlushDirty method hook?

For example:

public class HistoryHook implements Interceptor {

private Session cs;

public HistoryHook(Session s) {
cs=s;
}

public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException {
if(entity instanceof History) return false;
History h = new History(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types);
cs.save(h);
return false;
}
}


Will something like this work? If not - what would be your take to record and store change history using Hibernate?

Rihards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 7:14 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This works, but you can't use the same Session. Just pass in the Session as in your example, but actually use a new Session on the same connection, in Interceptor onFlushDirty:

Code:
Session newSession = getSessionFactory.openSession( oldSession.connection() );
newSession.save(foo);

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 7:15 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, and don't forget to flush() that new Session at the end. You don't have to commit or start any transaction (the connection already has one).

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 10:13 am 
Newbie

Joined: Tue Apr 13, 2004 6:41 am
Posts: 19
thanks christian.
have not tried it yet - are you sure it is going to be within same COMMIT?
or there are going to be two commits?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 10:16 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You just use the same connection, the first Session (and the "outside" code) still manages the transaction.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.