-->
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.  [ 6 posts ] 
Author Message
 Post subject: OnDelete event
PostPosted: Sat Mar 18, 2006 7:04 am 
Beginner
Beginner

Joined: Tue Oct 28, 2003 6:43 am
Posts: 33
Every time I delete an object (with a particular property setted) i need to:

1) create another object (using same Hibernate Session)
2) log operation with log4j
3) serialize object in a file system archive

How can I do this things with hibernate events?


Top
 Profile  
 
 Post subject: Re OnDelete event
PostPosted: Tue Mar 21, 2006 6:26 am 
Newbie

Joined: Fri Jul 15, 2005 12:42 am
Posts: 18
Hi
You need a cutom event listener like this
import org.hibernate.HibernateException;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.DeleteEventListener;
import org.hibernate.event.def.DefaultDeleteEventListener;

public class MyDeleteEventListener extends DefaultDeleteEventListener implements DeleteEventListener {

static final long serialVersionUID = -7260150090076059849L;

public void onDelete(DeleteEvent event) throws HibernateException {

//TODO

super.onDelete(event);
}

}



And you need to register the Listener like this
configuration = new Configuration();
SessionEventListenerConfig eventListenerConf = configuration.getSessionEventListenerConfig();
eventListenerConf.setDeleteEventListener(new MyDeleteEventListener());
sessionFactory = configuration.configure().buildSessionFactory();



Any piece of code you want to impl can be done in the onDelete method of the listener.
Hope this helps


Top
 Profile  
 
 Post subject: Ok, BUT ...
PostPosted: Tue Mar 21, 2006 6:47 am 
Beginner
Beginner

Joined: Tue Oct 28, 2003 6:43 am
Posts: 33
in ondelete event how can I use current HibernateSession?


Top
 Profile  
 
 Post subject: onDelete
PostPosted: Tue Mar 21, 2006 6:57 am 
Newbie

Joined: Fri Jul 15, 2005 12:42 am
Posts: 18
Current session can be obtained if you are using a Session Handler with a threadlocal implemented
private static final ThreadLocal threadSession = new ThreadLocal();
public static Session currentSession() {

log.debug("Entering method HibernateUtil:currentSession");

Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: "
+ getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new AxsRuntimeException(
AppErrorCodeConstants.
UNABLE_TO_GET_HIBERNATE_CURRENT_SESSION, ex);

}

log.debug("Exiting method HibernateUtil:currentSession");

return s;
}


Top
 Profile  
 
 Post subject: onDelete event
PostPosted: Tue Mar 21, 2006 7:11 am 
Newbie

Joined: Fri Jul 15, 2005 12:42 am
Posts: 18
download hibernate demo app from
http://caveatemptor.hibernate.org/2.html

It will have a file called HibernateUtil.java in which the approach mentioned before is implemented


Top
 Profile  
 
 Post subject: Great
PostPosted: Fri Mar 24, 2006 11:49 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 6:43 am
Posts: 33
But i've used event.getSession() (EventSource class) and it works!!!!!!!

Thanks


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