blob: 48583cb0abce2085e805621dd457b0ea5d3a6b85 [file] [log] [blame]
YelinCuifdf4ed72025-05-26 11:49:36 +08001package com.example.myproject.entity;
2
3
4
5import com.baomidou.mybatisplus.annotation.IdType;
6import com.baomidou.mybatisplus.annotation.TableId;
7
8import java.util.Objects;
9
10import lombok.Getter;
11import lombok.Setter;
12
13/**
14 * Base class for an entity, as explained in the book "Domain Driven Design".
15 * All entities in this project have an identity attribute with type Long and
16 * name id. Inspired by the DDD Sample project.
17
18 */
19@Setter
20@Getter
21public abstract class EntityBase {
22
23 /**
24 * This identity field has the wrapper class type Long so that an entity which
25 * has not been saved is recognizable by a null identity.
26 */
27 @TableId(type = IdType.AUTO)
28 private Integer id;
29
30 @Override
31 public boolean equals(final Object object) {
32 if (!(object instanceof EntityBase)) {
33 return false;
34 }
35 if (!getClass().equals(object.getClass())) {
36 return false;
37 }
38 final EntityBase that = (EntityBase) object;
39 _checkIdentity(this);
40 _checkIdentity(that);
41 return this.id.equals(that.getId());
42 }
43
44
45 private void _checkIdentity(final EntityBase entity) {
46 if (entity.getId() == null) {
47 throw new IllegalStateException("Comparison identity missing in entity: " + entity);
48 }
49 }
50
51 @Override
52 public int hashCode() {
53 return Objects.hash(this.getId());
54 }
55
56 @Override
57 public String toString() {
58 return this.getClass().getSimpleName() + "<" + getId() + ">";
59 }
60
61}