Writing Validation Code in the validateEntity Method

You can specify business logic by editing specific validation methods. You start by using the Entity Object Wizard to add methods to an entity object.

To write entity-level validation code, enter the protected validateEntity() method in your entity object's class file, call super.validateEntity(), and then enter your validation code.

For example:

protected validateEntity() { 
super.validateEntity()
// ### Implement custom domain validation logic here. ###
}

The Business Component for Java framework defines the signatures for these methods and generates skeleton code; you add the code to implement the validation logic. For example, given an Emp entity object, the framework could generate a validateEntity method to validate the Emp entity as a whole.

Use entity-level validation when an entity object's validity depends on two or more attributes, regardless of whether the attributes are related. For example, in a personnel-management application an Emp entity object might only be valid if the relationship between the DateHired attribute and the YearsOfService attribute is valid. Validation rules can also consider values from other entities: the following code example evaluates values from the Emp and Dept entity objects which are joined in a master-detail association.

public void validateEntity() { 
   // Generated call here.
   super.validateEntity();
  // Custom validation follows.  
  // Rule: Employees in ACCOUNTING cannot have Salary > 1500.
  if (this.getDept().getDname().equals("ACCOUNTING") &&
     (this.getSal().compareTo(1500) > 0) {
     throw new JboException("Salary too high for ACCOUNTING.");
  }
}

The following example method checks the values of three unrelated attributes. That is, none of the attribute values is derived from the others. However, none of these attributes can have a null value. If any of them is null, the method throws an exception.

protected void validateEntity() throws DataAccessException {
 // Generated call here.
 super.validateEntity();
 // Custom validation follows.
 //------------------ EDITCHECKS: HeaderId
 Number reqHdrId = (Number)getHeaderId("HeaderId");
 if (reqHdrId == null) {
  throw new JboException("HeaderId is required");
 }
 //------------------ EDITCHECKS: Segment1
 if ((getSegment1("segment1") == null) ||
  (((String)getSegment1("segment1")).trim().equals(""))) {
  throw new JboException("segment1 is required");
 }
 //------------------ EDITCHECKS: TypeLookupCode
 if ((getTypeLookupCode("typeLookupCode") == null) ||
  (((String)getTypeLookupCode("typeLookupCode")).trim().equals(""))) {
  throw new JboException("typeLookupCode is required");
 }
}