1 package org.oxerr.commons.user.domain;
2
3 import java.io.Serializable;
4 import java.time.Instant;
5 import java.util.UUID;
6
7 import javax.persistence.Access;
8 import javax.persistence.AccessType;
9 import javax.persistence.Column;
10 import javax.persistence.EntityListeners;
11 import javax.persistence.ForeignKey;
12 import javax.persistence.GeneratedValue;
13 import javax.persistence.Id;
14 import javax.persistence.JoinColumn;
15 import javax.persistence.ManyToOne;
16 import javax.persistence.MappedSuperclass;
17 import javax.persistence.Transient;
18 import javax.persistence.Version;
19 import javax.xml.bind.annotation.XmlTransient;
20
21 import org.springframework.data.annotation.CreatedBy;
22 import org.springframework.data.annotation.CreatedDate;
23 import org.springframework.data.annotation.LastModifiedBy;
24 import org.springframework.data.annotation.LastModifiedDate;
25 import org.springframework.data.jpa.domain.support.AuditingEntityListener;
26
27 @MappedSuperclass
28 @EntityListeners(AuditingEntityListener.class)
29 @Access(AccessType.PROPERTY)
30 public abstract class BaseEntity implements Serializable {
31
32 private static final long serialVersionUID = 2019062001L;
33
34 private UUID id;
35
36 private User createdBy;
37 private Instant createdDate;
38
39 private User lastModifiedBy;
40 private Instant lastModifiedDate;
41
42 private long version;
43
44 @Id
45 @Column(name = "id")
46 @GeneratedValue
47 public UUID getId() {
48 return id;
49 }
50
51 protected void setId(UUID id) {
52 this.id = id;
53 }
54
55 @CreatedBy
56 @ManyToOne
57 @JoinColumn(
58 name = "created_by",
59 updatable = false,
60 foreignKey = @ForeignKey(name = "fk_created_by")
61 )
62 public User getCreatedBy() {
63 return createdBy;
64 }
65
66 public void setCreatedBy(User createdBy) {
67 this.createdBy = createdBy;
68 }
69
70 @CreatedDate
71 @Column(
72 name = "created_date",
73 columnDefinition = "timestamp with time zone",
74 nullable = false,
75 updatable = false
76 )
77 public Instant getCreatedDate() {
78 return createdDate;
79 }
80
81 public void setCreatedDate(Instant createdDate) {
82 this.createdDate = createdDate;
83 }
84
85 @LastModifiedBy
86 @ManyToOne
87 @JoinColumn(
88 name = "last_modified_by",
89 foreignKey = @ForeignKey(name = "fk_last_modified_by")
90 )
91 public User getLastModifiedBy() {
92 return lastModifiedBy;
93 }
94
95 public void setLastModifiedBy(User lastModifiedBy) {
96 this.lastModifiedBy = lastModifiedBy;
97 }
98
99 @LastModifiedDate
100 @Column(
101 name = "last_modified_date",
102 columnDefinition = "timestamp with time zone",
103 nullable = false
104 )
105 public Instant getLastModifiedDate() {
106 return lastModifiedDate;
107 }
108
109 public void setLastModifiedDate(Instant lastModifiedDate) {
110 this.lastModifiedDate = lastModifiedDate;
111 }
112
113 @Version
114 @Column(name = "version", nullable = false)
115 public long getVersion() {
116 return this.version;
117 }
118
119 public void setVersion(long version) {
120 this.version = version;
121 }
122
123 @Transient
124 @XmlTransient
125 public boolean isNew() {
126 return getId() == null;
127 }
128
129 @Override
130 public boolean equals(Object obj) {
131 if (null == obj) {
132 return false;
133 }
134
135 if (this == obj) {
136 return true;
137 }
138
139 if (!getClass().equals(obj.getClass())) {
140 return false;
141 }
142
143 BaseEntity that = (BaseEntity) obj;
144
145 return null == this.getId() ? false : this.getId().equals(that.getId());
146 }
147
148 @Override
149 public int hashCode() {
150 int hashCode = 17;
151 hashCode += null == getId() ? 0 : getId().hashCode() * 31;
152 return hashCode;
153 }
154
155 }