How To Use Unique Constraints

In order to work with the unique constraints you will need to remember the following 3 steps.

1. Add an index for a field you wish to be unique:

Java:

configuration.objectClass(Item.class).objectField("field").indexed(true);



2. Configure a unique constraint:

Java:

configuration.add(new UniqueFieldValueConstraint(Item.class, "field"));



3. Handle unique constraint violation:

Java:
// open objectContainer
try {
  // do some work and save some objects
  objectContainer.commit();
} catch(UniqueFieldValueConstraintViolationException ex) {

  // log the error, notify the user
  objectContainer.rollback();
}