-->
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.  [ 2 posts ] 
Author Message
 Post subject: Validator, simple convention question
PostPosted: Wed Apr 27, 2011 6:08 pm 
Newbie

Joined: Fri Apr 08, 2011 6:24 am
Posts: 9
Hi,
I have some question about HibernateValidators (rather about convention):

1. I have i.e. addDate and modDate. I want to validate against addDate <= modDate. I know I have to write my own annotation but what about null?
I should check against null in isValid() or annotate it with @notNull (what is corect/good way)?
- isValid(gets refernce for one field) so I have to create new class i.e. Dates(addDate, modDate) and custom annotation for Dates, andagain, I should annotate add/modDate @notNull?

2. What about unique. Writing @Unique and check in isValid() what is in db tables is good or very bad idea?
This questions is generaly about "selecting" db in validators.


Top
 Profile  
 
 Post subject: Re: Validator, simple convention question
PostPosted: Thu Apr 28, 2011 3:56 am 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Quote:
1. I have i.e. addDate and modDate. I want to validate against addDate <= modDate. I know I have to write my own annotation but what about null?
I should check against null in isValid() or annotate it with @notNull (what is corect/good way)?
- isValid(gets refernce for one field) so I have to create new class i.e. Dates(addDate, modDate) and custom annotation for Dates, andagain, I should annotate add/modDate @notNull?


First regarding the null check, the correct way is to return true (which says that your constraint is valid) if one of your values is null and annotate your fields with the @NotNull annotation (for example in all HV constraint validator, null value is always considered as valid). To validate your two fields, you have to create your own class level annotation[1]. By doing this, you will be able to access these fields in your validator. If you want you can also use the HV built-in constraint named @ScriptAssert[2].

[1] http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#d0e332
[2] http://musingsofaprogrammingaddict.blogspot.com/2009/08/script-annotation-for-bean-validation.html (which is now included in the HV built-in constraints :)

Quote:
What about unique. Writing @Unique and check in isValid() what is in db tables is good or very bad idea?
This questions is generaly about "selecting" db in validators.


Look at this post http://community.jboss.org/wiki/AccessingTheHibernateSessionWithinAConstraintValidator. The problems which might occur are discussed here.

Hope this help.

--Kevin


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