-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate callback methods are not called
PostPosted: Sat Oct 31, 2009 7:17 pm 
Newbie

Joined: Fri Jun 19, 2009 9:31 am
Posts: 12
I read http://docs.jboss.org/hibernate/stable/ ... eners.html and tried out to annotate a method in my entity with a @PostUpdate annotation (I also tried the other annotations, like @PreRemove, @PostRemove...) with no luck: My callback method are isn't invoked.

My entity looks like this:
Code:
@Entity
class   XY {
  [...]
  @PostLoad
  public void test() {
    System.out.println("test() was called!");
  }
}   


What's going on here? Isn't that JPA feature supported by hibernate? Are there any other ways to define such callbacks for specific entities?


Top
 Profile  
 
 Post subject: Re: Hibernate callback methods are not called
PostPosted: Wed Nov 04, 2009 7:15 am 
Newbie

Joined: Fri Jun 19, 2009 9:31 am
Posts: 12
No comments? :(

To simplify my question: Is this feature in general supported by hibernate?


Top
 Profile  
 
 Post subject: Re: Hibernate callback methods are not called
PostPosted: Wed Nov 04, 2009 7:36 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Are you using JPA or plain hibernate?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Hibernate callback methods are not called
PostPosted: Wed Nov 04, 2009 8:56 am 
Newbie

Joined: Wed Nov 04, 2009 8:43 am
Posts: 2
The problem you're facing could be you haven't written listener class in which you should put the callback methods (like you written test()). After that you use that class in you entity class where you want to intercept using @EntityListeners annotation. something like this

>> it's my class where i put my callback methods

import javax.persistence.*;
public class MailNotifyListener {
@PostPersist
@PostLoad
public void notifyAdmin(Object entity) {
mail.send("Somebody saved or loaded: " + entity);
}
}

>> this is my actual entity where i want to get notification (notifyAdmin method will be executed) whenever the object get persisted.

import javax.persistence.*;
@Entity
@EntityListeners(MailNotifyListener.class)
public class Item {
...
@PreRemove
private void cleanup() {
...
}
}

please note down the @EntityListeners(MailNotifyListener.class), if you want to track any other entity class, all we have to do is simply add this annotation to that entity. hope this will help you. All the best!!


Top
 Profile  
 
 Post subject: Re: Hibernate callback methods are not called
PostPosted: Wed Nov 04, 2009 4:28 pm 
Newbie

Joined: Fri Jun 19, 2009 9:31 am
Posts: 12
Hey,

thanks for your answers!

@mmerder: I'm using hibernate. Are there any differences between hibernate and "plain hibernate"?

@kaviarasan14: I also tried to put the callbacks in another class and annotate my entity class with the @EntityListeners annotation; unfortunately without success. As described in the manual (http://docs.jboss.org/hibernate/stable/ ... eners.html) it should be possible to put the callbacks in the entity class and annotate them with annotations like @PostLoad.

Regards,
Jan


Top
 Profile  
 
 Post subject: Re: Hibernate callback methods are not called
PostPosted: Tue Nov 10, 2009 4:04 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, It's been a long time since I used plain hibernate and not JPA in a container. But as far as I remember, the entity listeners only work in a JPA environment. For plain hibernate try to use the hibernate event system: http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#objectstate-events

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Hibernate callback methods are not called
PostPosted: Tue Nov 10, 2009 9:34 am 
Newbie

Joined: Fri Jun 19, 2009 9:31 am
Posts: 12
Thanks for your answer. I already tried out the event system. In my opinion the drawback is, that the listeners are registered per session and not per entity. In result you have to test in the listener objects which entity is processed.


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