-
1. Re: Object level validation
norman.richards Dec 28, 2006 9:06 PM (in response to awhitford)You can easily add extra conditions:
@AssertTrue(message="second number must be larger than the first") public boolean checkNumbers() { System.out.println("number1=" + number1 + " number2=" + number2); return number2 > number1; }
Hibernate will check the condition at persist() time. Unfortunately, it doesn't handle it a way that is very friendly to your application. (from the perspective of being transparent) You'll have to add a bit of code to the application for that. Here's an overridden persist on EntityHome:@Transactional @Override public String persist() { ClassValidator validator = Validators.instance().getValidator(YourEntity.class, "yourEntity"); InvalidValue[] ivs = validator.getInvalidValues(getInstance()); if (ivs.length>0) { facesMessages.add(ivs); return null; } return super.persist(); }
-
2. Re: Object level validation
awhitford Dec 30, 2006 12:03 AM (in response to awhitford)I can see someone has a similar issue:
http://opensource.atlassian.com/projects/hibernate/browse/ANN-513
I though about using AssertTrue, but it felt forced, especially when I would need to use several if I wanted to actually link errors to certain fields...
Is there a way that I can create a ClassValidator for the Bond class, register it with Hibernate so that it always validates Bond objects before persisting? (I found little documentation on doing something like this, but did see a post that hinted at this notion.) -
3. Re: Object level validation
norman.richards Dec 30, 2006 12:30 AM (in response to awhitford)
You can override the Validators component to add your own createValidator() strategy. But I don't think that's really what you are after. If you think about what I posted, the problem is not in creating a class validator or having Hibernate call it. Hibernate does exactly that by default in JBoss. Add a validator to your entity and it WILL get called. The problem is in having the validation exception handled gracefully. Without the code I showed above, persist just throws a validation exception and there isn't a whole lot of you can do about it.