-->
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: Hibernate Validator: Annotation Functionality
PostPosted: Wed Jul 10, 2013 1:25 pm 
Newbie

Joined: Wed Jul 10, 2013 1:12 pm
Posts: 2
Hi

I am currently working on my master thesis using Hibernate Validator and it is really great. While working with the library, I came up with the following questions:

1) What is the reason that annotation parameters cannot take a variable e.g.
Code:
@Min(value = myValue)
where value is a field in the class where the annotation is used.

2) Would it be possible to extend Hibernate Validator with the functionality to define what object is used within the validator, e.g. something like this

Code:
public class MyBean {

  @Min(value = 18, object = this)
  private int age
  ...
}


The validator class of the Min constraint would have the following method:

Code:
public boolean isValid(MyBean object, ConstraintValidatorContext context)


Or without any specification, the whole object is always used, not the one where the annotation is used (i.e. do not distinguish between class level and property level annotations)

Thank you very much.
Oliver


Top
 Profile  
 
 Post subject: Re: Hibernate Validator: Annotation Functionality
PostPosted: Thu Jul 11, 2013 3:32 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Quote:
What is the reason that annotation parameters cannot take a variable


You can do something like @Min(value = myValue) as long as myValue is a constant expression. What you can't do is referring to non-constant fields or getters that way.

One might think of a way of supporting property references, e.g. @Min(expression="${myValue}"). But one of the major design philosophies in Bean Validation is type-safety, which isn't guaranteed when using this kind of expressions. BV therefore provides the concept of class-level constraints which allows implementing logic which depends on several properties of a bean in a type-safe way.

Quote:
Would it be possible to extend Hibernate Validator with the functionality to define what object is used within the validator


We just recently discussed this sort of functionality (see https://hibernate.atlassian.net/browse/HV-514) but decided in the end against it because we couldn't find a satisfying solution for the case of Validator#validateValue(). When using this method there is no bean instance (it's used to answer "was property foo of type Bar valid if it had value baz"). This would cause issues with constraint validators relying on the object being present. The discussion on the issue provides some more background. Just use a class-level constraint if you need to access the entire bean.

Btw, @Min(value = 18, object = this) is not valid in Java, you can't refer to an object from within an annotation like this.

Best luck with your thesis and don't hesitate to come up with more questions in case you need further clarification,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


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.