-->
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: Validate superclass property in subclass
PostPosted: Wed Feb 08, 2012 2:11 am 
Newbie

Joined: Wed Feb 08, 2012 2:09 am
Posts: 5
I have a superclass with property 'foo'. It has several subclasses. Only in some subclasses do I want to validate that foo is @NotNull

Is there a way to do it other than override the getter method?

Regards,
Ittay


Top
 Profile  
 
 Post subject: Re: Validate superclass property in subclass
PostPosted: Wed Feb 08, 2012 2:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Maybe default group sequences could be a solution. It depends really on your exact usecase. Maybe you could post some code? But my basic idea would be to redefine the default group sequence for some of the sub classes.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validate superclass property in subclass
PostPosted: Wed Feb 08, 2012 2:29 am 
Newbie

Joined: Wed Feb 08, 2012 2:09 am
Posts: 5
Thanks Hardy, here's a sample code:

Code:
abstract class Super {
  String foo;
  public String getFoo() {
    return foo;
  }
}

class Sub1 extends Super {

}

class Sub2 extends Super {

}

I want foo to be validated for @NotNull only in Sub1


Top
 Profile  
 
 Post subject: Re: Validate superclass property in subclass
PostPosted: Thu Feb 09, 2012 6:55 pm 
Hibernate Team
Hibernate Team

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

you could override getFoo() in Sub1 and put the constraint there instead adding it to the base class:

Code:
public abstract class Super {
  private String foo;
  public String getFoo() {
    return foo;
  }
}

class Sub1 extends Super {

  @Override
  @NotNull
  public String getFoo() {
    return super.getFoo();
  }

}

class Sub2 extends Super {

}


Hth,

--Gunnar

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


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.