Interface EntityDefinition


public interface EntityDefinition
Specifies an entity definition, encapsulating the metadata for an entity type.

An EntityDefinition contains all information about an entity's structure including:

  • The underlying table name and database mapping
  • All attributes (columns, foreign keys, derived attributes)
  • Primary key definition
  • Validation rules and behavior settings
  • Display properties (caption, description, ordering)

Entity definitions are typically created using the builder pattern during domain initialization:

public class Store extends DefaultDomain {

    // Define entity types
    interface Customer {
        EntityType TYPE = DOMAIN.entityType("store.customer");

        Column<Integer> ID = TYPE.integerColumn("id");
        Column<String> NAME = TYPE.stringColumn("name");
        Column<String> EMAIL = TYPE.stringColumn("email");
    }

    // Define the entity structure
    void defineCustomer() {
        EntityDefinition definition = Customer.TYPE.define(
                Customer.ID.define()
                    .primaryKey(),
                Customer.NAME.define()
                    .column()
                    .caption("Name")
                    .nullable(false)
                    .maximumLength(100),
                Customer.EMAIL.define()
                    .column()
                    .caption("Email")
                    .maximumLength(255))
            .tableName("customer")
            .caption("Customer")
            .orderBy(ascending(Customer.NAME))
            .smallDataset(true)
            .build();
    }
}
See Also:
  • Field Details

    • OPTIMISTIC_LOCKING

      static final PropertyValue<Boolean> OPTIMISTIC_LOCKING
      Specifies whether optimistic locking should be enabled by default for entities
      • Value type: Boolean
      • Default value: true
  • Method Details

    • type

      EntityType type()
      Returns:
      the entity type
    • tableName

      String tableName()
      Returns:
      the name of the underlying table, with schema prefix if applicable
    • condition

      ConditionProvider condition(ConditionType conditionType)
      Returns the ConditionProvider associated with the given type
      // Define custom condition types in entity definition
      interface Customer {
          EntityType TYPE = DOMAIN.entityType("customer");
          Column<String> STATUS = TYPE.stringColumn("status");
          Column<LocalDate> LAST_ORDER_DATE = TYPE.localDateColumn("last_order_date");
      
          // Custom condition for active customers
          ConditionType ACTIVE = ConditionType.custom("activeCustomers");
      }
      
      // In domain definition
      Customer.TYPE.define(
              Customer.STATUS.define()
                  .column(),
              Customer.LAST_ORDER_DATE.define()
                  .column())
          .condition(Customer.ACTIVE, (columns, values) ->
              // Returns customers with active status and recent activity
              Condition.and(
                  Customer.STATUS.equalTo("ACTIVE"),
                  Customer.LAST_ORDER_DATE.greaterThanOrEqualTo(LocalDate.now().minusMonths(6))
              ))
          .build();
      
      // Usage
      Condition activeCondition = Customer.ACTIVE.get();
      
      List<Entity> activeCustomers = connection.select(activeCondition);
      
      Parameters:
      conditionType - the condition type
      Returns:
      the condition provider associated with the given type
      Throws:
      IllegalArgumentException - in case no ConditionProvider is associated with the given conditionType
      See Also:
    • validator

      EntityValidator validator()
      Returns:
      the validator for this entity type
    • exists

      Predicate<Entity> exists()
      The default exists predicate returns true if the entity has a non-null original primary key, which is a best-guess about an entity existing in a database.
      Returns:
      the predicate to use to check if an entity of this type exists in the database
    • caption

      String caption()
      Returns:
      the caption to use when presenting entities of this type
    • description

      Optional<String> description()
      Returns:
      the entity description
    • smallDataset

      boolean smallDataset()
      Returns:
      true if the underlying table is small enough for displaying the contents in a combo box
    • readOnly

      boolean readOnly()
      Returns:
      true if this entity type is read only
    • optimisticLocking

      boolean optimisticLocking()
      Returns:
      true if optimistic locking should be used during updates
    • orderBy

      Optional<OrderBy> orderBy()
      Returns:
      the default order by clause to use when querying entities of this type, an empty Optional if none is available
    • selectTableName

      String selectTableName()
      Returns:
      the name of the table to use when selecting entities of this type
    • selectQuery

      Optional<EntitySelectQuery> selectQuery()
      Returns:
      the select query to use when selecting entities of this type, an empty Optional if none is available
    • stringFactory

      Function<Entity,String> stringFactory()
      Returns the function responsible for providing toString values for this entity type.
      // Define custom string representation
      Customer.TYPE.define(
              Customer.ID.define()
                  .primaryKey(),
              Customer.FIRST_NAME.define()
                  .column(),
              Customer.LAST_NAME.define()
                  .column(),
              Customer.EMAIL.define()
                  .column())
          .stringFactory(customer ->
              customer.get(Customer.LAST_NAME) + ", " +
              customer.get(Customer.FIRST_NAME) +
              " (" + customer.get(Customer.EMAIL) + ")")
          .build();
      
      // Usage
      Entity customer = entities.builder(Customer.TYPE)
          .with(Customer.FIRST_NAME, "John")
          .with(Customer.LAST_NAME, "Doe")
          .with(Customer.EMAIL, "john@example.com")
          .build();
      
      System.out.println(customer); // "Doe, John (john@example.com)"
      
      Returns:
      the function responsible for providing toString values for this entity type
    • cacheToString

      boolean cacheToString()
      Returns:
      true if the result of toString() is cached
    • comparator

      Comparator<Entity> comparator()
      Returns:
      the comparator used when comparing this entity type to other entities
    • attributes

      Returns:
      the EntityDefinition.Attributes instance
    • columns

      Returns:
      the EntityDefinition.Columns instance
    • foreignKeys

      Returns:
      the EntityDefinition.ForeignKeys instance
    • primaryKey

      Returns:
      the EntityDefinition.PrimaryKey instance
    • entity

      Entity entity()
      Creates a new Entity instance based on this definition
      Returns:
      a new Entity instance
    • entity

      Entity entity(Map<Attribute<?>,Object> values)
      Creates a new Entity instance based on this definition
      Parameters:
      values - the initial values, an empty map in case of no values
      Returns:
      a new Entity instance
      Throws:
      IllegalArgumentException - in case any of the value attributes are not part of the entity.
    • entity

      Entity entity(Map<Attribute<?>,Object> values, Map<Attribute<?>,Object> originalValues)
      Creates a new Entity instance based on this definition
      Parameters:
      values - the initial values, an empty map in case of no values
      originalValues - the original values, an empty map in case of no original values
      Returns:
      a new Entity instance
      Throws:
      IllegalArgumentException - in case any of the value attributes are not part of the entity.
    • primaryKey

      <T> Entity.Key primaryKey(T value)
      Creates a new Entity.Key instance based on this definition, initialised with the given value
      Type Parameters:
      T - the key value type
      Parameters:
      value - the key value, assuming a single value key
      Returns:
      a new Entity.Key instance
      Throws:
      IllegalStateException - in case the given primary key is a composite key
      IllegalArgumentException - in case the value is not of the correct type