-->
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: Extending existing annotation interfaces
PostPosted: Tue Jan 26, 2010 3:43 pm 
Newbie

Joined: Tue Jan 26, 2010 3:20 pm
Posts: 6
Hi All

I have spent a fair amount of time today trying to implement a new implementation of an existing validation annotation @Size. My use case means that I am validating a set of classes which wrap a string object in in an outer class:

Code:
public class SimpleElement {
    private String innerString = null;
....
}


My aim was to declare an annotation such as this

@Size(min=1)
private SimpleElement name = null;

and process it using code like this

Code:
public class SizeValidatorForSimpleElementType implements ConstraintValidator<Size, SimpleElementType>{
    private SizeValidatorForString sizeValidatorForString = null;

    @Override
    public void initialize(Size size) {
        this.sizeValidatorForString = new SizeValidatorForString();
        this.sizeValidatorForString.initialize(size);
    }

    @Override
    public boolean isValid(SimpleElementType value, ConstraintValidatorContext context) {
        return sizeValidatorForString.isValid(value == null ? null : value.getBodyText() , context);
    }
}


I was expecting to have to indicate to the factory in some way that it should supplement its standard mappings with an additional map of entries.

I have looked quite deeply and failed to find a methodology of doing this, the factory seems to use the org.hibernate.validator.metadata.ConstraintHelper class to map the entries. The factory also sees to implement a single instance of this class which it manages in a private manner so that it is not exposed in order that the inner mappings can be extended.

I am more than aware that the factory must have a method of mapping the interface to the implementations, I am though surprised that the factory does not allow the existing mappings to be extended or overridden.

Am I missing something, is there a deeply veiled way of doing this that I am missing, the alternative is that I implement my own identical version of the Size attribute and map it using the @Constraint annotation as per the examples, but this just seems daft to me.

If anyone can help and point me at a method of doing this I would appreciate it, I am also genuinely interested in the reasoning behind the code being non extensible in this way is there something in jsr303 that I have missed that prohibits this?

I appreciate any answers to this even if it is just to point me at at a thread I have failed to find in my numerous searches of the user groups.

Thanks In advance

Chris


Top
 Profile  
 
 Post subject: Re: Extending existing annotation interfaces
PostPosted: Wed Jan 27, 2010 4:06 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
The intended way to solve your use case is to add a xml configuration file with the following content:

Code:
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
                     xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
    <constraint-definition annotation="javax.validation.constraints.Size">
        <validated-by include-existing-validators="true">
            <value>fully.qualified.classname.for.SizeValidatorForSimpleElementType</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>


You specify the location and the name of this custom constraint mapping file in validation.xml using the constraint-mapping node.

Check the Validator online documentation as well - http://docs.jboss.org/hibernate/stable/ ... figuration

--Hardy


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.