简体   繁体   English

Spring 启动 - 实体:如何添加 CreatedDate(UTC 时区感知)、LastModifiedDate(UTC 时区感知)、CreatedBy、LastModifiedBy

[英]Spring boot - Entity : How to add CreatedDate (UTC timezone aware), LastModifiedDate (UTC timezone aware), CreatedBy, LastModifiedBy

I am new to Spring boot.我是 Spring 引导的新手。 I am planning to use postgresql database.我打算使用 postgresql 数据库。 I am using spring data JPA我正在使用 spring 数据 JPA

In this entity, I need to add these fields在这个实体中,我需要添加这些字段

@Entity
@Table(name="STUDENT")
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    
    @Column(name="STUDENT_NAME", length=50, nullable=false, unique=false)
    private String name;

    Here i want to add
    createdby, modifiedby <--
   
    createdat, modifiedat <-- (i want them to be timezone aware)
    
    // other fields, getters and setters
}

How can i do this.我怎样才能做到这一点。

I am coming from django background there we just do there我来自 django 背景,我们只是在那里

class Student(models.Model):

   ... other fields
   created_at = models.DateTimeField(auto_now_add=True)
   updated_at = models.DateTimeField(auto_now=True)
   created_by = models.ForeignKey(User,related_name="%(app_label)s_%(class)s_created_by",on_delete=models.CASCADE)

You can use the @PrePersist and @PreUpdate methods.您可以使用@PrePersist@PreUpdate方法。 For example:例如:

@Entity
public class SomeEntity {

    //..other properties..//

    private LocalDateTime createDate;
    private LocalDateTime modifyDate;
    private String createdBy;
    private String modifiedBy;

    @PrePersist
    private void beforeSaving() {
        createDate = LocalDateTime.now();
        createdBy = Thread.currentThread().getName(); //todo: put your logic here
    }

    @PreUpdate
    private void beforeUpdating() {
        modifyDate = LocalDateTime.now();
        modifiedBy = Thread.currentThread().getName(); //todo: put your logic here
    }
}

Additionally, if you wanna use such methods in all of your entities you can create BaseEntity and extend it in other classes, for example:此外,如果您想在所有实体中使用此类方法,您可以创建BaseEntity并在其他类中扩展它,例如:

@MappedSuperclass
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private LocalDateTime createDate;
    private LocalDateTime modifyDate;
    private String createdBy;
    private String modifiedBy;

    @PrePersist
    private void beforeSaving() {
        createDate = LocalDateTime.now();
        createdBy = Thread.currentThread().getName(); //todo: put your logic here
    }

    @PreUpdate
    private void beforeUpdating() {
        modifyDate = LocalDateTime.now();
        modifiedBy = Thread.currentThread().getName(); //todo: put your logic here
    }

    //..getters/setters omitted..//
}

and

@Entity
public class SomeEntity extends BaseEntity{
    //..other properties..//
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongodb 在 spring boot 中审计以保存 createdDate、lastModifiedDate、createdBy、lastModifiedBy - mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBy spring jpa auditing lastmodifiedby和lastmodifiedDate都可以,但createdBy和createdDate注释总是为null - spring jpa auditing lastmodifiedby and lastmodifiedDate are ok, but createdBy and createdDate annotation are always null 如何为 Spring Boot JPA 时间戳指定 UTC 时区 - How to specify UTC timezone for Spring Boot JPA Timestamp 如何在 Spring JPA 中为审计字段 @CreatedDate、@LastModifiedDate 以 UTC 格式保存时间戳 - How to save timestamp in UTC format for Audit fields @CreatedDate, @LastModifiedDate in Spring JPA 在使用 Spring Boot 自动转换 RequestBody 期间,ZonedDateTime 的时区更改为 UTC - Timezone of ZonedDateTime changed to UTC during auto conversion of RequestBody with Spring Boot 如何知道TimeZone与UTC的区别? - How to know TimeZone difference to UTC? 如何通过UTC偏移量获取时区? - How to get timezone by utc offset? Spring-boot @RequestParam批注将请求中的UTC时区转换为本地时区 - Spring-boot @RequestParam annotation converts UTC timezone in request to local timezone 时区问题:如果JVM时区在UTC中,则转换为UTC - Timezone issue: convert to UTC if JVM timezone is in UTC 这个日期是祖鲁 (UTC) 时区吗? - Is this date in Zulu (UTC) timezone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM