-->
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.  [ 9 posts ] 
Author Message
 Post subject: veto ing object save from interceptor
PostPosted: Thu Dec 14, 2006 9:53 pm 
Newbie

Joined: Thu Nov 09, 2006 8:46 pm
Posts: 7
The book says it is possible to veto object save from an interceptor ...can somebody give an example of how to do this ??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 6:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Return true from an interceptor method. This is expained in the API Javadoc of org.hibernate.Interceptor...

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 12:54 pm 
Newbie

Joined: Thu Nov 09, 2006 8:46 pm
Posts: 7
Quote:
Returns:
true if the user modified the state in any way.


is that what you are reffering to ?? .

I am not not "modifing" the object state in anyways i just want to veto the save on that object ...
I tried returning true when i wanted to veto the save but it didnt make any difference



As an alternative I can override performSaveOrUpdate method on DefaultSaveOrUpdateEventListener and not make super call if i want to veto the change ..

Also I gues i could make my domain model implement lifecycle ....

But i dont like any of the above solutions ... I want to use an interceptor ...

Suggestions ?? .


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 1:02 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use a listener, they are described in the book as well. I was not aware that these veto operations have been removed from the interceptor and moved to the event system. No idea why I missed that.

No changes required in the book though, I can't find the "return veto" statement you are referring to in your original post. Or, you have been reading Hibernate in Action, which I think included this.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 3:18 pm 
Newbie

Joined: Thu Nov 09, 2006 8:46 pm
Posts: 7
christian wrote:
Use a listener, they are described in the book as well. I was not aware that these veto operations have been removed from the interceptor and moved to the event system. No idea why I missed that.

Sure I will try that . Thanks

[quote=
No changes required in the book though, I can't find the "return veto" statement you are referring to in your original post. Or, you have been reading Hibernate in Action, which I think included this.[/quote]

No i am reading the new one ,

Page 553

"The org.hibernate.Interceptor interface also has many more methods that
you can use to hook into Hibernate’s processing. Most of them let you influence
the outcome of the intercepted operation; for example, you can veto the saving of
an object.
We think that interception is almost always sufficient to implement any
orthogonal concern."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 2:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
The old and deprecated LifeCycle interface explicit supports silent veto'ing by returning VETO.

Interceptor can veto by throwing an exception (and stop/breaks the session)

that has been the case since Hibernate 2.

In Hibernate 3 you have the event system that gives you full control (and responsibility)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 6:54 pm 
Newbie

Joined: Thu Nov 09, 2006 8:46 pm
Posts: 7
max wrote:
Interceptor can veto by throwing an exception (and stop/breaks the session)


I have a long running session ..which i dont want to break by throwing an exception...so i guess eventlistners are the way to go ...

so does this go into errata thread :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 12:57 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Added to errata.

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


Top
 Profile  
 
 Post subject: just to clarify
PostPosted: Wed Mar 28, 2007 6:34 am 
Newbie

Joined: Thu Jul 06, 2006 8:04 am
Posts: 14
just to clarify...is this the right approach to veto using event listeners?

I am looking how to veto changes using event listeners. As event listeners are more flexible than interceptors it also seemed more complicated. I began reading that they mimic the Session interface methods, so looked at extending the appropriate events for those methods which did something I wanted to veto. DefaultSaveOrUpdateEventListener and many others... Then I wasn't sure how to veto the changes anyway!

I then discovered at the end of ch12 (online reference) that the JAAS security implements the onPre...EventListeners. Although this code throws exceptions, the onPre...interface expects a boolean return value - which I found is the veto! - NB This wasn't in the documentation!! Return true to veto. Basically I think to veto all changes to the database, I need a class to implement preInsertEventListener, preUpdateEventListener, preDeleteEventListener and return true. Then I shall wire the class like the example - but each could refer to the same class implementing all the interfaces.

Code:
<listener type="pre-delete" class="org.hibernate.secure.JACCPreDeleteEventListener"/>
<listener type="pre-update" class="org.hibernate.secure.JACCPreUpdateEventListener"/>
<listener type="pre-insert" class="org.hibernate.secure.JACCPreInsertEventListener"/>


NB - note also that the 'pre-delete' names have no documentation on their mapping to the interfaces such as onPreDeleteEventListener. Although it doesn't take a genius to work out the mapping it could be a a little confusing sometimes as there is a mismatch between them in terms of numbers (eg what is post-commit-delete's listener). You can find out the names from a line in the dtd (http://hibernate.sourceforge.net/hibern ... on-3.0.dtd), They are
(auto-flush|merge|create|delete|dirty-check|evict|
flush|flush-entity|load|load-collection|lock|refresh|
replicate|save-update|save|update|pre-load|pre-update|
pre-insert|pre-delete|post-load|post-update|post-insert|
post-delete|post-commit-update|post-commit-insert|post-commit-delete)

I'm about to give it a go - but it all seems to fit. Just thought I'd add for those wanting to try it in the new event listener way. And give other an opportunity to correct me if this is not the way to do it!

adam


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