From 382a6c177f0f09fb90cebe0907982cfa64ad218a Mon Sep 17 00:00:00 2001 From: siujamo Date: Wed, 31 Dec 2025 14:55:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=BA=20JPA=20=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E6=B7=BB=E5=8A=A0=20life=20cycle=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../helix/domain/entity/Authority.java | 13 +++++++ .../helix/domain/entity/Department.java | 12 +++++++ .../onixbyte/helix/domain/entity/Menu.java | 12 +++++++ .../helix/domain/entity/Position.java | 12 +++++++ .../onixbyte/helix/domain/entity/Role.java | 8 ++--- .../helix/domain/entity/RoleAuthority.java | 5 +++ .../onixbyte/helix/domain/entity/Setting.java | 23 ++++++------ .../onixbyte/helix/domain/entity/User.java | 36 +++++++------------ .../helix/domain/entity/UserIdentity.java | 32 +++++++---------- .../helix/domain/entity/UserRole.java | 16 +++------ 10 files changed, 99 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Authority.java b/src/main/java/com/onixbyte/helix/domain/entity/Authority.java index eab02f1..3e3240f 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Authority.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Authority.java @@ -5,6 +5,7 @@ import jakarta.persistence.*; import org.hibernate.annotations.JdbcType; import org.hibernate.dialect.PostgreSQLEnumJdbcType; import org.springframework.security.core.GrantedAuthority; + import java.time.LocalDateTime; import java.util.Objects; @@ -261,4 +262,16 @@ public class Authority { public GrantedAuthority asGrantedAuthority() { return this::getCode; } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Department.java b/src/main/java/com/onixbyte/helix/domain/entity/Department.java index 1a68320..9a1bb1b 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Department.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Department.java @@ -287,4 +287,16 @@ public class Department implements Treeable { return new Department(id, name, parentId, sort, status, createdAt, updatedAt); } } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Menu.java b/src/main/java/com/onixbyte/helix/domain/entity/Menu.java index 5702892..ec7bed6 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Menu.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Menu.java @@ -224,4 +224,16 @@ public class Menu implements Treeable { ", updatedAt=" + updatedAt + '}'; } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Position.java b/src/main/java/com/onixbyte/helix/domain/entity/Position.java index 65db827..8f9b80b 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Position.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Position.java @@ -281,4 +281,16 @@ public class Position { return new Position(id, name, code, description, sort, status, createdAt, updatedAt); } } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Role.java b/src/main/java/com/onixbyte/helix/domain/entity/Role.java index 5fa4176..1896e98 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Role.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Role.java @@ -307,10 +307,10 @@ public class Role { } @PrePersist - private void onInsert() { - var currentTime = LocalDateTime.now(); - this.createdAt = currentTime; - this.updatedAt = currentTime; + private void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; } @PreUpdate diff --git a/src/main/java/com/onixbyte/helix/domain/entity/RoleAuthority.java b/src/main/java/com/onixbyte/helix/domain/entity/RoleAuthority.java index ff876ea..bcd00d8 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/RoleAuthority.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/RoleAuthority.java @@ -122,4 +122,9 @@ public class RoleAuthority { return new RoleAuthority(roleId, authorityId, createdAt); } } + + @PrePersist + protected void onCreate() { + this.createdAt = LocalDateTime.now(); + } } diff --git a/src/main/java/com/onixbyte/helix/domain/entity/Setting.java b/src/main/java/com/onixbyte/helix/domain/entity/Setting.java index 69f5046..967794a 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/Setting.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/Setting.java @@ -76,17 +76,6 @@ public class Setting { @Column(nullable = false) private LocalDateTime updatedAt; - @PrePersist - protected void onCreate() { - this.createdAt = LocalDateTime.now(); - this.updatedAt = LocalDateTime.now(); - } - - @PreUpdate - protected void onUpdate() { - this.updatedAt = LocalDateTime.now(); - } - public Setting() { } @@ -266,4 +255,16 @@ public class Setting { return new Setting(id, name, description, type, value, defaultValue, createdAt, updatedAt); } } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } \ No newline at end of file diff --git a/src/main/java/com/onixbyte/helix/domain/entity/User.java b/src/main/java/com/onixbyte/helix/domain/entity/User.java index 5d48206..f391a42 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/User.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/User.java @@ -121,30 +121,6 @@ public class User { @Column(nullable = false) private LocalDateTime updatedAt; - // --- JPA Lifecycle Callbacks for Auditing --- - - @PrePersist - protected void onCreate() { - if (this.createdAt == null) { - this.createdAt = LocalDateTime.now(); - } - if (this.updatedAt == null) { - this.updatedAt = LocalDateTime.now(); - } - - // Ensure status default is applied if not set - if (this.status == null) { - this.status = UserStatus.ACTIVE; - } - } - - @PreUpdate - protected void onUpdate() { - this.updatedAt = LocalDateTime.now(); - } - - // --- Constructors, Getters, Setters, and Builder (omitted for brevity) --- - public User() { } @@ -450,4 +426,16 @@ public class User { return new User(id, username, password, fullName, email, regionAbbreviation, phoneNumber, avatarUrl, status, departmentId, positionId, createdAt, updatedAt); } } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } \ No newline at end of file diff --git a/src/main/java/com/onixbyte/helix/domain/entity/UserIdentity.java b/src/main/java/com/onixbyte/helix/domain/entity/UserIdentity.java index 87d3e45..8181256 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/UserIdentity.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/UserIdentity.java @@ -40,26 +40,6 @@ public class UserIdentity { @Column(nullable = false) private LocalDateTime updatedAt; - // --- JPA Lifecycle Callbacks for Auditing --- - - @PrePersist - protected void onCreate() { - if (this.createdAt == null) { - this.createdAt = LocalDateTime.now(); - } - if (this.updatedAt == null) { - this.updatedAt = LocalDateTime.now(); - } - } - - @PreUpdate - protected void onUpdate() { - this.updatedAt = LocalDateTime.now(); - } - - - // --- Getters and Setters (Delegating to EmbeddedId) --- - /** * Gets the identifier of the internal user account from the composite primary key. * @return the user ID @@ -224,4 +204,16 @@ public class UserIdentity { return new UserIdentity(userId, provider, externalId, createdAt, updatedAt); } } + + @PrePersist + protected void onCreate() { + var createTime = LocalDateTime.now(); + this.createdAt = createTime; + this.updatedAt = createTime; + } + + @PreUpdate + protected void onUpdate() { + this.updatedAt = LocalDateTime.now(); + } } \ No newline at end of file diff --git a/src/main/java/com/onixbyte/helix/domain/entity/UserRole.java b/src/main/java/com/onixbyte/helix/domain/entity/UserRole.java index 794bf40..ec22695 100644 --- a/src/main/java/com/onixbyte/helix/domain/entity/UserRole.java +++ b/src/main/java/com/onixbyte/helix/domain/entity/UserRole.java @@ -37,17 +37,6 @@ public class UserRole { @Column(nullable = false, updatable = false) private LocalDateTime createdAt; - // --- JPA Lifecycle Callbacks for Auditing --- - - @PrePersist - protected void onCreate() { - if (this.createdAt == null) { - this.createdAt = LocalDateTime.now(); - } - } - - // --- Getters and Setters (Delegating to EmbeddedId and fields) --- - /** * Gets the identifier of the role from the composite primary key. * @@ -173,4 +162,9 @@ public class UserRole { return new UserRole(roleId, userId, createdAt); } } + + @PrePersist + protected void onCreate() { + this.createdAt = LocalDateTime.now(); + } } \ No newline at end of file