next up previous contents
Next: Referential constraints Up: Storing in the Database Previous: Many 2 Many   Contents

Adding constraints

Now that we have a class, which is stored in the DB we might like to add a field and constrain it to certain values. An example would be if we added a field called age which was an int, this field must be = 0 so an entry to place this constraint looks like this:
  <field name="age" type="integer">
    <sql name="user_age" constraint-name="user_age_gte_zero"
        constraint=">= 0"/>
This code will add a constraint on age such that an exception is thrown and the operation not performed if someone tries to set an age less than zero.