-->
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: Validation of Extended Classes
PostPosted: Tue Apr 12, 2011 7:44 pm 
Newbie

Joined: Tue Apr 12, 2011 7:33 pm
Posts: 4
Hi currently we have a mapped super class of the following:
Code:
@MappedSuperclass
public class PersonBase implements java.io.Serializable {

    protected String firstName;

    public PersonBase() {
    }

    @Column(name = "first_name", nullable = false, length = 32)
    @NotNull
    @Length(max = 32)
    public String getFirstName() {
        return this.firstName;
    }


    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    ...
}


These validations of length 32 reflect that required in the database.

We have however a extended class of this:

Code:
@Entity
@Table(name = "person")
public class PersonFraudCheck extends PersonBase {


    ...
   
    @Override
    @Transient
    @Column(name = "first_name", nullable = false, length = 15)
    @Length(max = 15)
    public String getFirstName() {
        return this.firstName;
    }
}


Where we want the validation length to be of length 15 rather than of 32.

This validation for PersonFraudCheck appears to be unpredictable, sometimes it validates to the requirement of 32 sometimes it requires 15.

What steps must be taken to ensure all validation for PersonFraudCheck is to 15?

Thanks so much for your time and appreciate any guidance.


Top
 Profile  
 
 Post subject: Re: Validation of Extended Classes
PostPosted: Wed Apr 13, 2011 5:43 am 
Hibernate Team
Hibernate Team

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

first of all, constraints don't override each other. @Length(max = 15) is not overriding the @Length(max = 32) constraints. It is just adding an additional constraint. If you validate an instance of PersonFraudCheck you are validating once against max=32 and once against max=15. Order is also not guaranteed. as per spec the constraint can be evaluated in any order. Only via a group sequence definition can one enforce an order.

One possible solution for your problem could be to work w/ groups and have the two different @Size constraints in different groups. Depending on your concrete circumstances this might or might not work.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validation of Extended Classes
PostPosted: Wed Apr 13, 2011 7:37 pm 
Newbie

Joined: Tue Apr 12, 2011 7:33 pm
Posts: 4
Hi Hardy!

Thanks that was extremely informative, helpful and straight to the point!

Sorry I am not familiar with the group validation that you are talking about could you kindly point me to some documentation in which you are referring to?

On another stream of thought...

If I have NO validation on PersonBase but rather have it on the extended classes would this be a suitable fix to the problem?

I.e.
PersonBase.java
Code:
    @Column(name = "first_name", nullable = false, length = 32)
    @NotNull
    public String getFirstName() {
        return this.firstName;
    }


ShortNamePerson.java extends PersonBase
Code:
    @Column(name = "first_name", nullable = false, length = 15)
    @NotNull
    @Length(max = 15)
    public String getFirstName() {
        return this.firstName;
    }



LongNamePerson.java extends PersonBase
Code:
    @Column(name = "first_name", nullable = false, length = 32)
    @NotNull
    @Length(max = 32)
    public String getFirstName() {
        return this.firstName;
    }


Top
 Profile  
 
 Post subject: Re: Validation of Extended Classes
PostPosted: Thu Apr 14, 2011 4:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Sorry I am not familiar with the group validation that you are talking about could you kindly point me to some documentation in which you are referring to?

Check the Hibernate Validator documentation - http://docs.jboss.org/hibernate/stable/ ... tml_single or if you prefer you
can have a look at the Bean Validation specification itself - http://www.jcp.org/en/jsr/detail?id=303

Quote:
If I have NO validation on PersonBase but rather have it on the extended classes would this be a suitable fix to the problem?

Sure. This will work. If it fits your use case.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validation of Extended Classes
PostPosted: Thu Apr 14, 2011 4:08 am 
Newbie

Joined: Tue Apr 12, 2011 7:33 pm
Posts: 4
Thanks Hardy! Extremely helpful and fast!


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.