-->
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.  [ 4 posts ] 
Author Message
 Post subject: Validation hierarchy
PostPosted: Tue Sep 13, 2011 12:02 pm 
Newbie

Joined: Tue Aug 21, 2007 10:35 am
Posts: 6
Hello, I'm developing a system with javax.validation and the hibernate-jpa-2.0-api.
I have the following scenrio:

Code:
@Entity
@Table(name="ath_login", uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
public class Login  {

   public enum Status {OK, NEW, BLOCKED, CLOSED}
   
   @Size(min=7, max=255)
        @NotNull
        @Column(name="name", length=255)
   protected String name;
   
   @Size(min=6, max=50)
   @Column(name="password", length=32)
   protected String password;

        // getters and setters
}

public class WebLogin extends Login {



}


I want that the name field of WebLogin class is validated by
@Pattern(regexp = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$" , message="Bad Email")

How can I do it?


Top
 Profile  
 
 Post subject: Re: Validation hierarchy
PostPosted: Tue Sep 13, 2011 2:11 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

you could put your constraints at the getters (e.g. getName()) of your properties instead of the fields. The constraints at the getters of one property in the hierarchy add up, so when annotating getName() in Login and WebLogin all these constraints will apply when validating an instance of WebLogin.

Annotating getters is preferable IMO anyways as the constraints are part of a type's public API that way, e.g. a user can see the constraints in the generated JavaDoc, which wouldn't be the case for constraints on private fields for instance.

You can find more information on constraint inheritence in the HV reference guide.

Hth, Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validation hierarchy
PostPosted: Tue Sep 13, 2011 3:10 pm 
Newbie

Joined: Tue Aug 21, 2007 10:35 am
Posts: 6
Thank you. I'll test it.
I put the validation on the atributes, because of hibernate. (It don't let me put onetomany on the getters).

By the way, as you know a lot of validation, tell me something. When the validation occurs? In the setter?


Top
 Profile  
 
 Post subject: Re: Validation hierarchy
PostPosted: Tue Sep 13, 2011 3:21 pm 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Hi,

I agree with Gunnar and as he says the getter approach is better IMO.

Btw I've seen that you want to validate an email. I don't know if you want to be independent of the underlying implementation of Bean Validation but Hibernate Validator provides an additional constraint for that (@Email).

Quote:
When the validation occurs? In the setter?

The validation is realized just before persisting, updating or removing the object in the data base. FYI, the following sentence comes from JPA 2 specification.

Quote:
This specification supports the use of bean validation for the automatic validation of entities upon the pre-persist, pre-update, and pre-remove lifecycle validation events.

Hope this help.

--Kevin


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