-->
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: enforcing mandatory properties in a subclass
PostPosted: Sun Jan 04, 2004 9:10 pm 
Newbie

Joined: Sat Dec 27, 2003 12:36 am
Posts: 3
Location: Brisbane, Australia
I have a class A and subclass B.

B has a mandatory (ie not null) attribute P.

Options I know of for modeling and enforcing the mandatory attribute in hiberate are:

1. Use joined-subclass, and set not-null="true" attribute of <property> or <column> elements (enforced by DB).

2. Use subclass, set not-null="false" and use an interceptor to validate the mandatory requirement of P on class B (enforced by appl call backs).

Are there any other options (particularly if I want to use subclass and avoid joined-subclass)?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 10:32 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Why do this at the DB or Hibernate-interceptor level?
Why not just enforce it on your POJOs as you would if you really were just using your domain model and actually enjoying the supposed benefits of transparent persistence?

For example, instead of this:
Code:
public class B extends A {
  private String p;

  public B(){
  }

  /**
   * @hibernate:property column="p_column_name" not-null="true"
   */
  public String getP(){
    return p;
  }

  public void setP(String _p) {
    this.p = _p;
  }
}

have this:
Code:
public class B extends A {
  private String p;

  /** Hibernate use only **/
  protected B(){
  }

  public B(String _p){
    setP(_p);
  }

  /**
   * @hibernate:property column="p_column_name" not-null="true"
   */
  public String getP(){
    return p;
  }

  /**
   * @throws IllegalArgumentException if _p is null
   */
  public void setP(String _p) {
    if( _p == null ){
      throw IllegalArgumentException("cannot set p attribute to null");
      // or if you wanted, you could throw a checked exception
    }
    this.p = _p;
  }
}


Isn't this the kind of thing that we should do if we really are using transparent persistence? As opposed to relying on the correct configuration of your DB to catch the error (via eferential integrity checking) or some interceptor/validatable interface/AOP jiggery-pokery.

Although, all of that said, I'd personally still have the not-null constraint defined on the DB :)

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 11:32 pm 
Newbie

Joined: Sat Dec 27, 2003 12:36 am
Posts: 3
Location: Brisbane, Australia
Thanks for your suggestion re enforement in POJO. I agree with this approach as the model is then reflected in the POJO interface which is good.

I don't think the not-null constraint can be defined on the DB, as P is not a required attribute of the base class A. Do you know of any other way of enforcing this at the DB level (ie a constraint that is conditional on another field in the same row??)

FYI: I've been defining hbm files and using hbm2java and hbm2ddl to generate. A couple of points (

hbm2ddl tool note: If you define property P in class B as not-null then the schema will include not null for P which works for B but not for A - ie the subclass semantics are 'enforced' upon the superclass. I'm not sure this is a problem with the schema generation tool but I didn't expect this. (this assumes single table per subclass)

hbm2java tool note: Would it make sense to have hbm2java generate POJOs with this behaviour?

I suppose my ideal is to be able to define this in hbm (ie in one place) and then have each of the tools generate appropriately and possibly have it enforced at both levels.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 12:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I opened the bug to Jira
http://opensource.atlassian.com/project ... key=HB-602

If you can add a simple test case to this issue :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 6:58 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Quote:
hbm2java tool note: Would it make sense to have hbm2java generate POJOs with this behaviour?

I'd like this too. Can we vote? :)


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.