Hello, Your sample code seems to do more work than necessary for the result it finds. Every Node returned by the Path should be non-null, therefore your whole test could be reduced to violation.getPropertyPath().iterator().hasNext().
I'm not sure the above actually gives you exactly what it is you're looking for. If you simply want to know whether the violated constraint was declared as a class level constraint of the root bean, then it works. But to use the Business/Employee model as an example, if you need to differentiate between a constraint declared on the Employee class as opposed to one declared on Business.manager (of type Employee), your approach will fall short. It appears to me as though (violation.getLeafBean() == violation.getInvalidValue()) should be true when the constraint was declared on the bean class. Matt On Thu, Oct 10, 2013 at 4:44 AM, Umesh Awasthi <[email protected]>wrote: > I need to determine type of constraint violation from > [ConstraintViolation][1] object. > one way is to use `ConstraintViolation#getPropertyPath()`. If `getName()` > returns null on the `leaf` node you have a class level constraint, > otherwise a property level constraint. > > > One option is like > > Iterator<Node> violationNodes=violation.getPropertyPath().iterator(); > Node leafNode=null; > while (violationNodes.hasNext()){ > leafNode=violationNodes.next(); > } > > if(leafNode!=null){ > // property constraint > } > else{ > // class constraint > } > > > Is this good approach to determine or there can be other efficient or good > approach to do this? > > -- > With Regards > Umesh Awasthi > http://www.travellingrants.com/ > > >
