Repository navigation
Expand file tree
/
Copy pathBaseModel.java
More file actions
80 lines (61 loc) · 1.68 KB
/
Copy pathBaseModel.java
File metadata and controls
80 lines (61 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.raysmond.blog.models;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import javax.persistence.*;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.util.Date;
/**
* An abstract base model class for entities
*
* @author Raysmond
*/
@MappedSuperclass
public abstract class BaseModel implements Comparable<BaseModel>, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(nullable = false)
private ZonedDateTime createdAt;
@Column(nullable = false)
private ZonedDateTime updatedAt;
@PrePersist
public void prePersist() {
createdAt = updatedAt = ZonedDateTime.now();
}
@PreUpdate
public void preUpdate() {
updatedAt = ZonedDateTime.now();
}
@Override
public int compareTo(BaseModel o) {
return this.getId().compareTo(o.getId());
}
public boolean equals(Object other) {
if (other == null || other.getClass() != this.getClass()) {
return false;
}
return this.getId().equals(((BaseModel) other).getId());
}
public int hashCode() {
return new HashCodeBuilder().append(getId()).toHashCode();
}
public Long getId() {
return id;
}
public void setId(Long _id) {
id = _id;
}
public ZonedDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(ZonedDateTime createdAt) {
this.createdAt = createdAt;
}
public ZonedDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(ZonedDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}