JDO : Bean Validation

The Bean Validation API (JSR0303) can be hooked up with JDO (DataNucleus extension) so that you have validation of an objects values prior to persistence, update and deletion. To do this

  • Put the javax.validation "validation-api" jar in your CLASSPATH, along with the Bean Validation implementation jar of your choice (Apache BVAL, Hibernate Validator, etc)
  • Set the persistence property datanucleus.validation.mode to one of auto , none (default), or callback
  • Optionally set the persistence property(s) datanucleus.validation.group.pre-persist , datanucleus.validation.group.pre-update , datanucleus.validation.group.pre-remove to fine tune the behaviour
  • Use JDO as you normally would for persisting objects

To give a simple example of what you can do with the Bean Validation API

@PersistenceCapable
public class Person 
{
    @PrimaryKey
    @NotNull
    private Long id;

    @NotNull
    @Size(min = 3, max = 80)
    private String name;

    ...
}

So we are validating that instances of the Person class will have an "id" that is not null and that the "name" field is not null and between 3 and 80 characters. If it doesn't validate then at persist/update an exception will be thrown.

Note that this was added to DataNucleus AccessPlatform in version 3.0.3