-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJpaBaseEntity.java
55 lines (43 loc) · 1.3 KB
/
JpaBaseEntity.java
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
package com.wypl.jpacommon;
import java.time.LocalDateTime;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.wypl.common.exception.WyplException;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@MappedSuperclass
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public abstract class JpaBaseEntity {
@CreatedDate
@Column(name = "created_at", nullable = false, updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
@Column(name = "modified_at", nullable = false)
private LocalDateTime modifiedAt;
@Column(name = "deleted_at")
private LocalDateTime deletedAt;
public void delete() {
if (isDeleted()) {
throw new WyplException(JpaErrorCode.ALREADY_DELETED_ENTITY);
}
this.deletedAt = LocalDateTime.now();
}
public void restore() {
if (isNotDeleted()) {
throw new WyplException(JpaErrorCode.NON_DELETED_ENTITY);
}
this.deletedAt = null;
}
public boolean isNotDeleted() {
return deletedAt == null;
}
public boolean isDeleted() {
return !isNotDeleted();
}
}