-->
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.  [ 8 posts ] 
Author Message
 Post subject: Validate properties of a superclass
PostPosted: Mon Aug 22, 2011 6:16 am 
Newbie

Joined: Tue Jul 05, 2011 7:28 am
Posts: 10
Hi all,

I have an entity class that inherits from a base class that contains properties such as Id, CreateDate, etc. I cannot modify this base class so my question is: How can I place validation constraints on these properties from my sub class? e.g. the CreateDate cannot be in the past. I know I could create a custom constraint that I place at class level on my sub class or I could configure it through XML but I was wondering if there is any other ways of placing validation constraints on properties of superclasses from your sub class. I am using Hibernate Validator 4.2.0.Final.

Thanks,
M


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Mon Aug 22, 2011 9:09 am 
Hibernate Team
Hibernate Team

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

using the Bean Validation API only you have to use XML. Using Hibernate Validator you could also use the programmatic constraint API. But in one of the cases you are placing the constraints on the subclass.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Mon Aug 22, 2011 11:50 am 
Newbie

Joined: Tue Jul 05, 2011 7:28 am
Posts: 10
Thanks Hardy for the reply.

I don't know if I'm taking the correct approach but I was thinking of implementing something like the following:

@ConstraintOverride(name="createDate", constraints = {@NotNull, @...})
public class MyEntity extends BaseEntity {
...
}

My entity classes are currently using constraint annotations so I'd like to continue to use that pattern rather than having some constraints in an XML file and some as annotations. Also I know I could create a custom constraint to validate the createDate property and place it on the MyEntity class but I'd like something a bit more generic that I could basically apply my constraints to any property of a superclass without having to modify the superclass.

I'd appreciate your input on the direction I am taking or am I over complicating the solution?

Thanks,
Mark


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Mon Aug 22, 2011 4:14 pm 
Hibernate Team
Hibernate Team

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

that's an interesting idea, I'm afraid though that it's not really possible to implement this in the generic way you'd like to.

The problem is the "constraints" member of your @ConstraintOverride annotation which you can't define it in a generic way (it's not possible to have an array of java.lang.annotation.Annotation as annotation member). So you would have to specify the constraints in a textual way which would be very error-prone.

But even if this were possible I think there are some questions. In case there are several sub-types of BaseEntity, would the @NotNull constraint for createDate be defined on every of these sub-types or only one? The first would be very redundant, the second probably quickly becoming complex (how to determine all constraints applying to BaseEntity?).

All in all I think using the XML descriptor or the programmatic API is the approach you likely should take.

Hth,

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Tue Aug 23, 2011 5:45 am 
Hibernate Team
Hibernate Team

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

I don't really have much to add, except I think Gunnar already gave you the best advice :-)


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Tue Aug 23, 2011 7:48 am 
Newbie

Joined: Tue Jul 05, 2011 7:28 am
Posts: 10
Thanks Gunnar and Hardy for your input.

Just one last question - when you say "the programmatic API approach", I presume you mean creating a custom constraint validator for the property in the superclass that I want to validate?

e.g.

@ValidCreateDate
public class MyEntity extends BaseEntity {
...
}

Thanks again,
Mark


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Tue Aug 23, 2011 11:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
What me mean is the Hibernate Validator specific programmatic constraint definition API - http://docs.jboss.org/hibernate/stable/ ... ammaticapi


Top
 Profile  
 
 Post subject: Re: Validate properties of a superclass
PostPosted: Sat Sep 03, 2011 1:19 pm 
Newbie

Joined: Sun Feb 27, 2011 11:40 am
Posts: 8
Haven't seen this option mentioned yet -- couldn't you place the annotations on overridden getters?

Code:
class MyEntity extends BaseEntity {
    @NotNull
    @...
    @Override
    public Date getCreateDate() {
        return super.getCreateDate();
    }
}


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