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 :)