Interface Entity<I>

  • Type Parameters:
    I - the type of the entity identifier.
    All Known Subinterfaces:
    AggregateRoot<I>

    @DomainEntity
    public interface Entity<I>
    An entity represent a thread of continuity and identity, going through a lifecycle, though its attributes may change. It is not defined primarily by its attributes but by its identity that stays the same through time and across distinct representations.

    The identity of an entity must be unique and immutable. It must be chosen carefully and well defined in the model. Identification can come from:

    • The outside: a user of the system can provide the identity, handling the uniqueness himself.
    • The inside: the entity can generate its own identity using an algorithm.
    • An IdentityGenerator.

    An entity should not be merely a holder of attributes, but should also contain the behavior that is directly relevant to it. Do not create entities with only getters and setters but add methods with meaningful business names, implementing domain behavior.

    The BaseEntity class can be used as a base class for domain entities. It provides an implementation of the getId(), equals(Object) and hashCode() methods.

    Example:

     public class SomeEntity implements Entity<SomeEntityId> {
         private SomeEntityId id;
    
         public SomeEntity(SomeEntityId id) {
             this.id = id;
         }
    
        @Override
         public SomeEntityId getId() {
             return this.id;
         }
    
        @Override
         public int hashCode() {
             // implement using identity attribute only
         }
    
        @Override
         public boolean equals() {
             // implement using identity attribute only
         }
    
         // Other methods
     }
     
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      boolean equals​(Object other)
      As per Domain-Driven Design semantics, entity equality must be computed on its identity only, as returned by the getId() method.
      I getId()
      Returns the identifier of this entity instance.
      int hashCode()
      As per Domain-Driven Design semantics, the hash code of an entity must be computed on its identity only, as return by as returned by the getId() method.
    • Method Detail

      • getId

        I getId()
        Returns the identifier of this entity instance.
        Returns:
        the entity identifier.
      • equals

        boolean equals​(Object other)
        As per Domain-Driven Design semantics, entity equality must be computed on its identity only, as returned by the getId() method. Two entities from the same class hierarchy (with an inheritance relationship between them) can be checked for equality as they have a comparable identity.

        When implementing this method for entities, the semantics above must be respected in addition to the semantics of Object.equals(Object).

        Overrides:
        equals in class Object
        Parameters:
        other - other object
        Returns:
        true if the other object is an entity of the same class hierarchy as this entity and has an identity equal to this entity identity.
      • hashCode

        int hashCode()
        As per Domain-Driven Design semantics, the hash code of an entity must be computed on its identity only, as return by as returned by the getId() method.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code value for this entity.