-->
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.  [ 1 post ] 
Author Message
 Post subject: How to ValidateForAction in NHibernate
PostPosted: Fri Jul 29, 2005 12:47 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!
I have worked with other O/R Mappers (for example NextStep EOF) and in them, it is possible to create methods that act as invariants:

-Validate Before Update
-Validate Before Insert
-Validate Before Delete
-Validate Before Load

This way it is possible to ensure that your object has consisten data before each "persistencey involved" action. This validations act as constraints to prevent the processing of inconsistent data (for example, a Person with a BirthDate greater than a MarryDate, or an Order related with zero Products)

In EOF this was possible because all persisten object inherit from a class that has this methods defined, and when the EditionContext (more/less equivalent with NHibernate session/transaction) was asked to persist all changes, it automatically excecuted the appropiate method to validate for action that was goingo to be performed...

Now.. in NHibernate documentation in Chapter 4 it says " Unlike the callback methods of the Lifecycle interface, validate() might be called at unpredictable times. The application should not rely upon calls to validate() for business functionality." So... the documentation clearly state that validate cannot be used for this...

Maybe the callback methods of the Lifecycle interface...

public interface Lifecycle {
public boolean onSave(Session s) throws CallbackException;
public boolean onUpdate(Session s) throws CallbackException;
public boolean onDelete(Session s) throws CallbackException;
public void onLoad(Session s, Serializable id);
}

This seems a lot like what I need.. but I have some questions...

1) Sometimes one wants to know the violations of all the dirty objects in memory, in that case, maybe sending an exception in this methods is not the best options... why? because then on their first inconsistent object, the process stops... so the maybe it is better to "save" with fields or properties of which objects are inconsisten, and when the last dirty object is verified, then throw the exception to prevent the changes from reaching the database... do you recommend a particula place to write the code to do this? perhaps inheriting form Session and and creaing a "ValidatableSession"?
2) From Documentation: "If onSave(), onUpdate() or onDelete() return true, the operation is silently vetoed" that means... "the operation for that particular object is vetoed? or all the operation (for all dirty objects) is vetoed"?
3) From Documentation "onSave(), onDelete() and onUpdate() may be used to cascade saves and deletions of dependent objects. This is an alternative to declaring cascaded operations in the mapping file." So.. if I choose to use this methods I cannot use mapping file based cascade operations? or can they coexist?
4) From Documentation "onSave(), onDelete() and onUpdate() [...] may not be used to load dependent objects since the Session interface may not be invoked from inside this method." So... I cannot use this to validate that the relationship of this object is consistent (from bussines rules point of view)... for example, to prevent the deletion of a Product if it has Orders related to it, but only if those Order were not cancelled (their status field is different from "C")... then... what is the recommended way to perform this kind of validation?


Thanks
bye


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.