-->
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.  [ 3 posts ] 
Author Message
 Post subject: Validation across collection
PostPosted: Mon Mar 12, 2012 4:53 pm 
Newbie

Joined: Thu May 01, 2008 3:16 pm
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Validation across collection
PostPosted: Thu Mar 15, 2012 5:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

first off I would recommend to update your versions to Hibernate Validator 4 (Bean Validation) and Hibernate 4 (or at least 3.6). A lot of things have changed in Hibernate, in particular the handling of collection change events. I can not promise this your usecase would work out of the box, but your chances are definitely better.
At the very least I would use Hibernate Validator 4. The HV 3 code base is obsolete. I don't think the validation itself is the problem, but rather the integration of the validation, but it is hard to tell. You definitely get more help if you upgrade.

Another tip could be to look at the different change event listeners. Provided collection change events are already fired in 3.3 you could maybe write your own event listener and trigger the validation from there.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validation across collection
PostPosted: Thu Mar 15, 2012 3:50 pm 
Newbie

Joined: Thu May 01, 2008 3:16 pm
Posts: 2
Thanks Hardy,

Yea, I know we definitely need to upgrade, but that's probably a few months away.
After looking at HV 4.0, I figured it would probably fix my problem, but in the mean time
I did find a work around. I found out that the validator doesn't fire on cascade (I don't know why it wouldn't),
but I implemented a new SaveUpdate listener and then fired the validation from there. Works good enough
for now.

Thanks again!


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