김태오

JpaAuditing 본문

Spring Boot

JpaAuditing

ystc1247 2023. 4. 10. 16:47

I was having this issue on the createdAt and modifiedAt not being created automatically upon posting a new entity. It had worked flawlessly in my previous project, yet when I tried to post a new entity in a postman test, it gave me the 500 Internal Server Error.

 

package com.keyworld.projectboard.domain;

import lombok.Getter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@Getter
@ToString
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class AuditingFields {

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    @CreatedDate
    @Column(nullable = false, updatable = false)
    protected LocalDateTime createdAt; // 생성일시

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    @LastModifiedDate
    @Column(nullable = false)
    protected LocalDateTime modifiedAt; // 수정일시
}

This was what my class that I overrided into my entity class to manage the LocalDateTime for createdAt and modifiedAt. I had thought the error above was due to the constructor or setter in the DTO class or the request class that I used to pass the form-data in my post controller.

  public static ArticleDto of(String author, String title, String content, String password,Boolean adm ) {
        return new ArticleDto(null, author, title, content, password,adm, LocalDateTime.now(), LocalDateTime.now());
    }

This was the constructor I refactored, not knowing this was not the issue. I tried to give the values of createdAt and modifiedAt the current LocalDateTime of my EST when the attributes were not provided upon posting the entity. This was far from the issue; with googling and looking into my previous project that had worked with the proper logic related to the automatic creation of the Time attribute, I found that I missed the annotation "@EnableJpaAuditing" in my main class of the application.

@EnableJpaAuditing

@EnableJpaAuditing is an annotation that enables JPA auditing in a Spring Boot application. It adds the necessary beans and configurations to the Spring application context to enable auditing features, such as @CreatedDate, @CreatedBy, @LastModifiedDate, and @LastModifiedBy annotations.

When this annotation is present in a Spring Boot application configuration class, Spring Data JPA will automatically populate auditing-related fields in entities with the current date/time and user information, based on the configured auditing strategy. This allows you to easily keep track of when and by whom an entity was created or modified.

 

The @CreatedDate etc also had aliases in Hibernate, which had the similar functionality to Spring Data JPA.

Spring Data JPA Hibernate
@CreatedAt @CreationTimeStamp
@LastModifiedDate @UpdateTimeStamp

This error fixing process reminded me to take close looks to the Lombok annotations, knowing their uses and functionality before using them in the project.

'Spring Boot' 카테고리의 다른 글

FeignClient 사용시 PATCH method 가 안되는 오류  (0) 2024.03.11
async 작업 시 thread 의 traceId 전파  (0) 2024.03.09
동일성과 동등성  (0) 2023.11.29
@Getter 와 @Setter 남용  (0) 2023.11.02
Parameter handling in Controllers  (0) 2023.07.15