refactor: 为 JPA 实体添加 life cycle 配置

This commit is contained in:
siujamo
2025-12-31 14:55:11 +08:00
parent d25c754bf0
commit 382a6c177f
10 changed files with 99 additions and 70 deletions
@@ -5,6 +5,7 @@ import jakarta.persistence.*;
import org.hibernate.annotations.JdbcType; import org.hibernate.annotations.JdbcType;
import org.hibernate.dialect.PostgreSQLEnumJdbcType; import org.hibernate.dialect.PostgreSQLEnumJdbcType;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Objects; import java.util.Objects;
@@ -261,4 +262,16 @@ public class Authority {
public GrantedAuthority asGrantedAuthority() { public GrantedAuthority asGrantedAuthority() {
return this::getCode; 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();
}
} }
@@ -287,4 +287,16 @@ public class Department implements Treeable<Long> {
return new Department(id, name, parentId, sort, status, createdAt, updatedAt); 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();
}
} }
@@ -224,4 +224,16 @@ public class Menu implements Treeable<Long> {
", updatedAt=" + updatedAt + ", updatedAt=" + updatedAt +
'}'; '}';
} }
@PrePersist
protected void onCreate() {
var createTime = LocalDateTime.now();
this.createdAt = createTime;
this.updatedAt = createTime;
}
@PreUpdate
protected void onUpdate() {
this.updatedAt = LocalDateTime.now();
}
} }
@@ -281,4 +281,16 @@ public class Position {
return new Position(id, name, code, description, sort, status, createdAt, updatedAt); 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();
}
} }
@@ -307,10 +307,10 @@ public class Role {
} }
@PrePersist @PrePersist
private void onInsert() { private void onCreate() {
var currentTime = LocalDateTime.now(); var createTime = LocalDateTime.now();
this.createdAt = currentTime; this.createdAt = createTime;
this.updatedAt = currentTime; this.updatedAt = createTime;
} }
@PreUpdate @PreUpdate
@@ -122,4 +122,9 @@ public class RoleAuthority {
return new RoleAuthority(roleId, authorityId, createdAt); return new RoleAuthority(roleId, authorityId, createdAt);
} }
} }
@PrePersist
protected void onCreate() {
this.createdAt = LocalDateTime.now();
}
} }
@@ -76,17 +76,6 @@ public class Setting {
@Column(nullable = false) @Column(nullable = false)
private LocalDateTime updatedAt; 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() { public Setting() {
} }
@@ -266,4 +255,16 @@ public class Setting {
return new Setting(id, name, description, type, value, defaultValue, createdAt, updatedAt); 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();
}
} }
@@ -121,30 +121,6 @@ public class User {
@Column(nullable = false) @Column(nullable = false)
private LocalDateTime updatedAt; 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() { 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); 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();
}
} }
@@ -40,26 +40,6 @@ public class UserIdentity {
@Column(nullable = false) @Column(nullable = false)
private LocalDateTime updatedAt; 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. * Gets the identifier of the internal user account from the composite primary key.
* @return the user ID * @return the user ID
@@ -224,4 +204,16 @@ public class UserIdentity {
return new UserIdentity(userId, provider, externalId, createdAt, updatedAt); 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();
}
} }
@@ -37,17 +37,6 @@ public class UserRole {
@Column(nullable = false, updatable = false) @Column(nullable = false, updatable = false)
private LocalDateTime createdAt; 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. * Gets the identifier of the role from the composite primary key.
* *
@@ -173,4 +162,9 @@ public class UserRole {
return new UserRole(roleId, userId, createdAt); return new UserRole(roleId, userId, createdAt);
} }
} }
@PrePersist
protected void onCreate() {
this.createdAt = LocalDateTime.now();
}
} }