-->
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.  [ 11 posts ] 
Author Message
 Post subject: Validation and inheritance - does not "work"?!
PostPosted: Mon Feb 07, 2011 8:32 am 
Newbie

Joined: Thu Dec 01, 2005 12:38 pm
Posts: 12
Hi all,

currently i'm struggling with validating "java beans" that uses inheritance.

Consider the example "person and driver" out of the documentation:
http://docs.jboss.org/hibernate/validat ... tiongroups

The class Driver extends class Person. I'm wondering that the field "age" is *not* declared within person!? I have a similar situation here:

There are some super-classes with some validation constraints. My sub-class has its own validation constraints. These constraints are field-constraints on fields, that are declared on the super class! These classes *and* its constraints are defined by different developers! Problem: I cannot have multiple configuration files with constraints for one bean!?

How do you arrive with something like this?

Best regards
Daniel


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Mon Feb 07, 2011 9:21 am 
Hibernate Team
Hibernate Team

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

if I understand correctly you are implementing against some classes over which you have no control (you are using them as library!?). In particular you are extending some of the classes and want to control constraint validation - either overwriting existing ones or adding new ones. Is this your usecase? If so you will have to use xml mapping files. Using xml configuration standalone or in combination with annotations should give you the configuration flexibility you need.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Mon Feb 07, 2011 11:04 am 
Newbie

Joined: Thu Dec 01, 2005 12:38 pm
Posts: 12
Yes, i want to *add* new constraints. The contraints for the super-class are defined in xml configuration, too!


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Mon Feb 07, 2011 11:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
So the library is containing a validation.xml you want to override/ignore?
The best way of doing this would be to use your own bootstrapping and use Configuration.ignoreXmlConfiguration to ignore META-INF/validation.xml. Then you use Configuration.addMapping to pass an input stream to your own configuration.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 5:10 am 
Newbie

Joined: Thu Dec 01, 2005 12:38 pm
Posts: 12
Hi Hardy,

i have to *extend* the existing mappings. I want to add my specialized constraints.
We are using Configuration#addMapping() already, becaouse we have various configurations depending on our business logic.

Daniel


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 5:16 am 
Hibernate Team
Hibernate Team

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

this is all very abstract. I am not even sure anymore whether you still have a problem or whether you already found a solution. Maybe you can post a concrete use-case/example explaining your constraints, packaging, problems, etc

As a thought, have you considered ignoring the constraints shipped by the library and just adding them through your own configuration (using ignoreXmlConfiguration)?

--Hardy


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 6:26 am 
Newbie

Joined: Thu Dec 01, 2005 12:38 pm
Posts: 12
Ok, you're right, an example is helpful here:

Classes Person and Driver. Consider the declaration of the field age is on the class Person!
Code:
public class Person {
   
    private String name;
    public int age;

    // getters and setters ...
}

public class Driver extends Person{

private boolean hasDrivingLicense;

// getters and setters ...
}

Constraints for class Person.
Code:
constraints for super-class
<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">

    <bean class="Car" ignore-annotations="true">

        <field name="name">
            <constraint annotation="javax.validation.constraints.NotNull"/>
        </field>

        <field name="age">
            <constraint annotation="javax.validation.constraints.Min">
                <element name="value">1</element>
            </constraint>
        </field>

    </bean>   
</constraint-mappings>

Constraints for class Driver; These contraints should be an addition to the constraints of class Person!
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">

    <bean class="Driver" ignore-annotations="true">

      <field name="age">
            <constraint annotation="javax.validation.constraints.Min">
                <element name="value">18</element>
            </constraint>
       </field>

    </bean>   
</constraint-mappings>


I hope you get my problem now. Unfortunately i have no solution for this.

Daniel


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 7:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you tried using <getter> for the age property in the Driver configuration?


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 8:04 am 
Newbie

Joined: Thu Dec 01, 2005 12:38 pm
Posts: 12
This will work probably, but it seems to be a workaround. With a "bean like" approach, it shoud be possible to do this, without overwriting all needed getters?!


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 8:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I think you are right and I think I found the error - http://opensource.atlassian.com/project ... wse/HV-430


Top
 Profile  
 
 Post subject: Re: Validation and inheritance - does not "work"?!
PostPosted: Tue Feb 08, 2011 9:00 am 
Newbie

Joined: Thu Dec 01, 2005 12:38 pm
Posts: 12
Very good, thanks for your help! I'm looking forward to "4.2.0.CR1" :-)

Daniel


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