-->
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: determine which annotation throw the message
PostPosted: Mon Jun 20, 2011 11:17 am 
Newbie

Joined: Fri Aug 15, 2008 3:33 am
Posts: 17
Hi,

I'm working with the Hibernate Validator 4.1 in a JavaEE application (ICEfaces, Hibernate core, EJB3 etc.)

I determine if a property is valid or not with the following code:
Code:
@SuppressWarnings("unchecked")
ClassValidator classValidator = new ClassValidator(validatedBean.getClass());
@SuppressWarnings("unchecked")
InvalidValue[] validationMessages = classValidator.getInvalidValues(validatedBean, validatedProperty);


But I want to determine which annotation threw the error message if there are more than one annotation for the same property.
Is this possible? And how?

Many thanks


Top
 Profile  
 
 Post subject: Re: determine which annotation throw the message
PostPosted: Mon Jun 20, 2011 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
According to the types in your example it seems like you're actually using Hibernate Validator 3.x.

This is the legacy version, you should definitely use HV 4.x nowadays, which is the reference implementation of JSR 303 ("Bean Validation"). Best is to start with HV 4.2 which happens to be released today :-)

Using HV 4.x and Bean Validation you would find out the invalid annotation like this:

Code:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<MyBean>> violations = validator.validate(new MyBean());
Class<? extends Annotation> annotationType = violations.iterator().next().getConstraintDescriptor().getAnnotation().annotationType();

Hth, Gunnar

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


Top
 Profile  
 
 Post subject: Re: determine which annotation throw the message
PostPosted: Tue Jun 21, 2011 4:21 am 
Newbie

Joined: Fri Aug 15, 2008 3:33 am
Posts: 17
Hi Gunnar,

Thank you for your prompt answer. I saw that I made a mistake saying that I'm using version 4.1 and instead I'm using Hibernate Validator version 3.1. I'm stick with this version because the production server for my application is WebSphere Application Server 6.1. And I was not able to use the Hibernate core v3.5 with WAS6.1 and for this reason I downgraded to Hibernate Core 3.3 and Hibernate Validator 3.1.


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.