Base class for an entity, as explained in the book "Domain Driven Design". All entities in this project have an identity attribute with type Long and name id. Inspired by the DDD Sample project. @author Christoph Knabe @author plexpt @see <a href= "https://github.com/citerus/dddsample-core/blob/mas
| 23 | * @since 2017-03-06 |
| 24 | */ |
| 25 | @Setter |
| 26 | @Getter |
| 27 | public abstract class EntityBase { |
| 28 | |
| 29 | /** |
| 30 | * This identity field has the wrapper class type Long so that an entity which |
| 31 | * has not been saved is recognizable by a null identity. |
| 32 | */ |
| 33 | @TableId(type = IdType.AUTO) |
| 34 | private Integer id; |
| 35 | |
| 36 | @Override |
| 37 | public boolean equals(final Object object) { |
| 38 | if (!(object instanceof EntityBase)) { |
| 39 | return false; |
| 40 | } |
| 41 | if (!getClass().equals(object.getClass())) { |
| 42 | return false; |
| 43 | } |
| 44 | final EntityBase that = (EntityBase) object; |
| 45 | _checkIdentity(this); |
| 46 | _checkIdentity(that); |
| 47 | return this.id.equals(that.getId()); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Checks the passed entity, if it has an identity. It gets an identity only by |
| 52 | * saving. |
| 53 | * |
| 54 | * @param entity the entity to be checked |
| 55 | * @throws IllegalStateException the passed entity does not have the identity |
| 56 | * attribute set. |
| 57 | */ |
| 58 | private void _checkIdentity(final EntityBase entity) { |
| 59 | if (entity.getId() == null) { |
| 60 | throw new IllegalStateException("Comparison identity missing in entity: " + entity); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int hashCode() { |
| 66 | return Objects.hash(this.getId()); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
| 71 | return this.getClass().getSimpleName() + "<" + getId() + ">"; |
| 72 | } |
| 73 | |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected