I
- the type of the entity identifier.@DomainEntity public interface Entity<I>
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:
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 }
Modifier and Type | Method and 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. |
I getId()
boolean equals(Object other)
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)
.
Copyright © 2013-2018–2019 SeedStack. All rights reserved.