Gunnar asked me an interesting question on Bean Validation. Instead of keeping the knowledge private, I thought it would be useful to share more wildly.

Is it possible to determine whether a given constraint is specified at field or property level using the constraint metadata API?

First off, in many case you don't need to do the distinction. Had Java support properties from the ground up, we would not have this problem (sigh).

Anyways, the answer is yes. You can fine tune what is returned from the metadata API.

PropertyDescriptor property = 

Set<ConstraintDescriptor<?>> fieldConstraints =

Set<ConstraintDescriptor<?>> propertyConstraints =

The key here is the use of the findConstraints() fluent API. You have three ways to restrict the metadata retrieved:

  • declaredOn(ElementType... types): defines where to look the constraints (METHOD, FIELD etc)
  • lookingAt(Scope scope): defines whether to look for constraints hosted on superclass/interfaces (default) or not
  • unorderedAndMatchingGroups(Class<?>... groups): restrict to the constraints matching a given set of groups for this element (note that the ordering of group sequences is not respected)

That's all.

18. Aug 2010, 17:08 CET | Link
Is it possible to support i18n for the validations exceptions messages ???
19. Aug 2010, 12:14 CET | Link

Which exception message specifically. The exception returning the list of constraint failure contains the i18n failure messages.

23. Aug 2010, 22:04 CET | Link
Arthur Ronald F D Garcia | arthurseveral(AT)

Hi Emmanuel,

Althoug what i said has nothing to do with this entry, i really advice you as JSR-303 leader (and its expert group) that the @Valid annotation must support groups attribute. If @Valid annotation can be applied into ElementType.PARAMETER as @Target, so it must support groups as attribute. Spring MVC, for instance, can take @Valid but as i can not supply which group (or groups) i want to test, i need to implement my own vendor-specific validation if i want to call some specific group (or groups)

Think about it

best regards Arthur Ronald F D Garcia

24. Aug 2010, 19:25 CET | Link

Hi Arthur, I knew we would have to add such a feature but the semantic at the time was not crystal clear and that's why we did not have it in the spec initially. Please open or vote for the associated issue