Package st.orm

Class IntegrityViolationException

All Implemented Interfaces:
Serializable

public class IntegrityViolationException extends PersistenceException
Thrown when a value breaks a rule of its table: a write the database refuses for a constraint, a null in a non-nullable field at write time, or a value that validateOrThrow finds too large for its column.

The database refuses a write for one constraint at a time, so a violation it reports comes alone; validateOrThrow reports every violation of the entity at once. Each IntegrityViolation names the kind of rule and the fields it resolves to, so a caller matches on a metamodel key:


 try {
     users.insert(user);
 } catch (IntegrityViolationException e) {
     if (e.violations(User_.email).stream().anyMatch(v -> v instanceof IntegrityViolation.Unique)) {
         // The email is taken.
     }
     throw e;
 }
 

A violation the database reported passes through the template's exception mapper like any failure the database reported, with the driver's exception as its cause, so a mapper translates it to its own type.

Since:
1.16
See Also:
  • Constructor Details

    • IntegrityViolationException

      public IntegrityViolationException(String message, List<IntegrityViolation> violations)
      Constructs a new exception with the specified detail message and violations.
      Parameters:
      message - the detail message.
      violations - the violations, at least one.
    • IntegrityViolationException

      public IntegrityViolationException(String message, List<IntegrityViolation> violations, @Nullable Throwable cause)
      Constructs a new exception with the specified detail message, violations and cause.
      Parameters:
      message - the detail message.
      violations - the violations, at least one.
      cause - the failure the database reported, or null when Storm found the violations itself.
  • Method Details

    • violations

      public List<IntegrityViolation> violations()
      Returns the violations. A violation the database reported comes alone; validateOrThrow reports every violation of the entity.
      Returns:
      the violations, at least one.
    • violations

      public List<IntegrityViolation> violations(Metamodel<?,?> field)
      Returns the violations that resolve to the given field.
      Parameters:
      field - the field, as a metamodel key.
      Returns:
      the violations on the field; empty when none resolves to it.