I'm currently using Validator 3.1.0 and Hibernate 3.3.2. I've implemented a custom validator, however it is only firing on insert. I have an idea of why and wondered if anybody could confirm.
I have a class of "InvoiceItemDefinition" (with custom validator) and it has a collection of "InvoiceItemAllocation" (each of these has a percentage that is allocated). My business validation is that when the InvoiceItemDefinition is saved, the percentage values on all the InvoiceItemAllocation objects must add to 100. It seems that the validator fires on insert because the InvoiceItemDefinition object is being inserted for the first time, however, if there are subsequent changes to the collection (adding/removing of items, changing of percentages), the validator does not fire again. My guess is that the InvoiceItemDefinition hasn't changed so the validator doesn't fire.
Is this what's happening and if so, is there a way to work around this type of validation?
Thanks!
---- UPDATE -- I just updated the parent object and saved and that triggered the validation, so my suspicions were apparently right, (there is no validation of the parent if just the collection changes). Once, again though, given this is the way it works, how do you handle a business validation scenario above?
I don't like having to implement some "save" method because that still makes it the developers job to call the save method every time to validate instead of just letting hibernate load and save the object and always handle validation.
|