Class DefaultEntities

java.lang.Object
is.codion.framework.domain.entity.DefaultEntities
All Implemented Interfaces:
Entities, Serializable

public abstract class DefaultEntities extends Object implements Entities, Serializable
A default Entities implementation.
See Also:
  • Field Summary

    Fields inherited from interface is.codion.framework.domain.entity.Entities

    STRICT_DESERIALIZATION, VALIDATE_FOREIGN_KEYS
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Instantiates a new DefaultEntities for the given domainType
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final void
    add(EntityDefinition definition)
     
    builder(EntityType entityType)
    Creates a new Entity.Builder instance for the given entityType
    Entities entities = domain.entities();
    
    // Build an entity with initial values
    Entity customer = entities.builder(Customer.TYPE)
        .with(Customer.NAME, "John Doe")
        .with(Customer.EMAIL, "john@example.com")
        .with(Customer.ACTIVE, true)
        .build();
    
    // Build with a foreign key reference
    Entity order = entities.builder(Order.TYPE)
        .with(Order.CUSTOMER_FK, customer)
        .with(Order.DATE, LocalDate.now())
        .with(Order.TOTAL, 100.50)
        .build();
    
    final boolean
    contains(EntityType entityType)
     
    definition(EntityType entityType)
    Returns the EntityDefinition for the given entityType
    definition(String entityTypeName)
    Returns the EntityDefinition for the given entityType name
    Returns all EntityDefinitions found in this Entities instance
     
    final Entity
    entity(EntityType entityType)
    Creates a new empty Entity instance of the given entityType
    Entities entities = domain.entities();
    
    // Create an empty entity
    Entity customer = entities.entity(Customer.TYPE);
    
    // Set values individually
    customer.set(Customer.NAME, "John Doe");
    customer.set(Customer.EMAIL, "john@example.com");
    
    // The entity is mutable and tracks changes
    customer.set(Customer.NAME, "John Lennon");
    customer.modified(Customer.NAME); // true
    
    final <T> Entity.Key
    primaryKey(EntityType entityType, T value)
    Creates a new Entity.Key instance of the given entityType, initialised with the given value
    Entities entities = domain.entities();
    
    // Create a single-value primary key
    Entity.Key customerKey = entities.primaryKey(Customer.TYPE, 42);
    
    // Use the key to fetch an entity
    Entity customer = connection.selectSingle(customerKey);
    
    // Keys can be compared
    Entity.Key anotherKey = entities.primaryKey(Customer.TYPE, 42);
    customerKey.equals(anotherKey); // true
    
    // Null values are allowed
    Entity.Key nullKey = entities.primaryKey(Customer.TYPE, null);
    
    final <T> List<Entity.Key>
    primaryKeys(EntityType entityType, T... values)
    Creates new Entity.Key instances of the given entityType, initialised with the given values
    Entities entities = domain.entities();
    
    // Create multiple keys at once
    List<Entity.Key> customerKeys = entities.primaryKeys(Customer.TYPE, 1, 2, 3, 4, 5);
    
    // Fetch multiple entities
    List<Entity> customers = connection.select(customerKeys);
    
    // Varargs syntax allows flexible usage
    List<Entity.Key> keys = entities.primaryKeys(Order.TYPE,
        "ORD-001", "ORD-002", "ORD-003");
    
    final String
     
    protected final void
    validateForeignKeys(boolean validateForeignKeys)
    Specifies whether to validate foreign keys when created, asserting that the referenced entity has been defined.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • DefaultEntities

      protected DefaultEntities(DomainType domainType)
      Instantiates a new DefaultEntities for the given domainType
      Parameters:
      domainType - the domainType
  • Method Details

    • domainType

      public final DomainType domainType()
      Specified by:
      domainType in interface Entities
      Returns:
      the DomainType this Entities instance is associated with
    • definition

      public final EntityDefinition definition(EntityType entityType)
      Description copied from interface: Entities
      Returns the EntityDefinition for the given entityType
      Specified by:
      definition in interface Entities
      Parameters:
      entityType - the entityType
      Returns:
      the entity definition
    • definition

      public final EntityDefinition definition(String entityTypeName)
      Description copied from interface: Entities
      Returns the EntityDefinition for the given entityType name
      Specified by:
      definition in interface Entities
      Parameters:
      entityTypeName - the name of the entityType
      Returns:
      the entity definition
    • contains

      public final boolean contains(EntityType entityType)
      Specified by:
      contains in interface Entities
      Parameters:
      entityType - the entityType
      Returns:
      true if this domain contains a definition for the given type
    • definitions

      public final Collection<EntityDefinition> definitions()
      Description copied from interface: Entities
      Returns all EntityDefinitions found in this Entities instance
      Specified by:
      definitions in interface Entities
      Returns:
      all entity definitions
    • entity

      public final Entity entity(EntityType entityType)
      Description copied from interface: Entities
      Creates a new empty Entity instance of the given entityType
      Entities entities = domain.entities();
      
      // Create an empty entity
      Entity customer = entities.entity(Customer.TYPE);
      
      // Set values individually
      customer.set(Customer.NAME, "John Doe");
      customer.set(Customer.EMAIL, "john@example.com");
      
      // The entity is mutable and tracks changes
      customer.set(Customer.NAME, "John Lennon");
      customer.modified(Customer.NAME); // true
      
      Specified by:
      entity in interface Entities
      Parameters:
      entityType - the entityType
      Returns:
      a new Entity instance
    • builder

      public final Entity.Builder builder(EntityType entityType)
      Description copied from interface: Entities
      Creates a new Entity.Builder instance for the given entityType
      Entities entities = domain.entities();
      
      // Build an entity with initial values
      Entity customer = entities.builder(Customer.TYPE)
          .with(Customer.NAME, "John Doe")
          .with(Customer.EMAIL, "john@example.com")
          .with(Customer.ACTIVE, true)
          .build();
      
      // Build with a foreign key reference
      Entity order = entities.builder(Order.TYPE)
          .with(Order.CUSTOMER_FK, customer)
          .with(Order.DATE, LocalDate.now())
          .with(Order.TOTAL, 100.50)
          .build();
      
      Specified by:
      builder in interface Entities
      Parameters:
      entityType - the entityType
      Returns:
      a new Entity.Builder
    • primaryKey

      public final <T> Entity.Key primaryKey(EntityType entityType, T value)
      Description copied from interface: Entities
      Creates a new Entity.Key instance of the given entityType, initialised with the given value
      Entities entities = domain.entities();
      
      // Create a single-value primary key
      Entity.Key customerKey = entities.primaryKey(Customer.TYPE, 42);
      
      // Use the key to fetch an entity
      Entity customer = connection.selectSingle(customerKey);
      
      // Keys can be compared
      Entity.Key anotherKey = entities.primaryKey(Customer.TYPE, 42);
      customerKey.equals(anotherKey); // true
      
      // Null values are allowed
      Entity.Key nullKey = entities.primaryKey(Customer.TYPE, null);
      
      Specified by:
      primaryKey in interface Entities
      Type Parameters:
      T - the key value type
      Parameters:
      entityType - the entityType
      value - the key value, assumes a single value key
      Returns:
      a new Entity.Key instance
    • primaryKeys

      public final <T> List<Entity.Key> primaryKeys(EntityType entityType, T... values)
      Description copied from interface: Entities
      Creates new Entity.Key instances of the given entityType, initialised with the given values
      Entities entities = domain.entities();
      
      // Create multiple keys at once
      List<Entity.Key> customerKeys = entities.primaryKeys(Customer.TYPE, 1, 2, 3, 4, 5);
      
      // Fetch multiple entities
      List<Entity> customers = connection.select(customerKeys);
      
      // Varargs syntax allows flexible usage
      List<Entity.Key> keys = entities.primaryKeys(Order.TYPE,
          "ORD-001", "ORD-002", "ORD-003");
      
      Specified by:
      primaryKeys in interface Entities
      Type Parameters:
      T - the key value type
      Parameters:
      entityType - the entityType
      values - the key values, assumes a single value key
      Returns:
      new Entity.Key instances
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • validateForeignKeys

      protected final void validateForeignKeys(boolean validateForeignKeys)
      Specifies whether to validate foreign keys when created, asserting that the referenced entity has been defined. Disable in case of cyclical dependencies.
      Parameters:
      validateForeignKeys - true if foreign keys should be validated
    • add

      protected final void add(EntityDefinition definition)
      Parameters:
      definition - the entity definition to add
      Throws:
      IllegalArgumentException - in case this DefaultEntities instance already contains the given definition