-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to Validate a Collection of objects in XML
PostPosted: Mon May 21, 2012 3:21 pm 
Newbie

Joined: Mon May 21, 2012 3:05 pm
Posts: 3
I was looking over this post of using JSR-303 to validate a collection of objects. The solution works great with annotations, but I can't seem to get it to work with the Hibernate Validator XML formatted configuration.

For example, I have code similar to this:

Code:
public class DataSet
{
    Collection<Data> dataCollection;
    public Collection<Data> getDataCollection() {...}
}


From there, I have a custom validator/annotation DataValidator/@ValidData.

In XML, I do this first:

Code:
<bean class="DataSet"
    ignore-annotations="true">
    <field name="dataCollection">
        <valid/>
        <constraint annotation="ValidData"/>
    </field>
</bean>


However, I get the following exception:

Code:
Exception in thread "main" javax.validation.UnexpectedTypeException: No validator could be found for type: java.util.Collection<DataSet>


So I swap the <valid> tag with the <constraint> one in the XML. It seems this is not valid with the XSD schema and the XML can no longer be parsed.

Code:
<bean class="DataSet"
    ignore-annotations="true">
    <field name="dataCollection">
        <constraint annotation="ValidData"/>
        <valid/> <!-- moved this down a line -->
    </field>
</bean>


Code:
Exception in thread "main" javax.validation.ValidationException: Error parsing mapping file.


Does anyone know how I can use XML to validate this collection with must custom validator?

P.S. If anyone is sure about their solution, feel free to get some points on Stack Overflow -- have it cross posted here.


Top
 Profile  
 
 Post subject: Re: How to Validate a Collection of objects in XML
PostPosted: Tue May 22, 2012 5:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

looking at your error message I think your problem is not in XML, but in DataValidator. Are you sure it can handle java.util.Collection<DataSet>? Can you post the code for your DataValidator?

--Hardy


Top
 Profile  
 
 Post subject: Re: How to Validate a Collection of objects in XML
PostPosted: Tue May 22, 2012 8:50 am 
Newbie

Joined: Mon May 21, 2012 3:05 pm
Posts: 3
Hi Hardy,

Good thought, however, I've tried both ways. The current workaround is to have the DataValidator take a Collection<Data> object, but ideally it should be able to just take a Data object so that I can reuse the validator for individual Data objects (rather than just Collections of them). For example, I'm looking for the XML equivalent of this annotation set with:

Code:
public class DataSet
{
    Collection<Data> dataCollection;

    @ValidData
    @Valid
    public Collection<Data> getDataCollection() {...}
}


Here's the original DataValidator that I want to work, followed by the not-reusable workaround:

Code:
public class DataValidator implements ConstraintValidator<ValidData, Data>

  public void initialize(ValidData constraintAnnotation){...}
  public boolean isValid(Data value, ConstraintValidatorContext context){...}
}


The workaround (that I'm trying to get away from since it's not reusable for individual Data objects):

Code:
public class DataValidator implements ConstraintValidator<ValidData, Collection<Data>>

  public void initialize(ValidData constraintAnnotation){...}
  public boolean isValid(Collection<Data> value, ConstraintValidatorContext context){...}
}


Thanks!


Top
 Profile  
 
 Post subject: Re: How to Validate a Collection of objects in XML
PostPosted: Tue May 22, 2012 9:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
What you want in this case is a class constraint on Data. Instead of placing @ValidData on the collection you should place it on class level of Data and only use @Valid (resp. <valid/> in xml) for the collection. Placing @ValidData on the collection WILL NOT apply the onstraint on each element of the collection.

--Hardy


Top
 Profile  
 
 Post subject: Re: How to Validate a Collection of objects in XML
PostPosted: Tue May 22, 2012 10:50 am 
Newbie

Joined: Mon May 21, 2012 3:05 pm
Posts: 3
Thanks Hardy - that was exactly it!

The key was adding a class-level constraint annotation in the XML to the Data POJO itself. The <class> tag inside <bean> wasn't terribly well documented in the XML portion of the HV docs, but was in the XSD itself.

Here was the solution:

Code:
    <bean class="DataSet"
        ignore-annotations="true">
        <field name="dataCollection">
            <valid/>
           
        </field>
    </bean>

    <bean class="Data" ignore-annotations="true">
         <class>
              <constraint annotation="ValidData"/>
         </class>
    </bean>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.