프로젝트 복기 - 엔티티

c.Hano·2025년 2월 21일

프로그래머스

목록 보기
3/5

해당 테이블을 기준으로 calendar 엔티티를 작성하였다.


package reproject.calendars.entity;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor
public class Calendar {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String description;

    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    public Calendar(String name, String description) {
        this.name = name;
        this.description = description;
        this.createdAt = LocalDateTime.now();
    }
    
    public void update(String name, String description) {
    	this.name = name;
        this.description = description;
        this.updatedAt = LocalDateTime.now();
    }
}

@Entity , @Id, @GeneratedValue(strategy = GenerationType.IDENTITY)

  • @Entity 에너테이션이 있을 경우 Pr키를 설정하는 @Id 에너테이션이 필수이다.
  • @GeneratedValue(strategy = GenerationType.IDENTITY)
    • 다른 데이터 값이 들어올때 해당 에너테이션이 붙은 값을 자동으로 1씩 올려준다.

@NoArgsConstructor

  • 자바에선 생성자 생성이 필수이다. 매개변수를 받지 않는 기본 생성자가 있을 시 자동으로 생성자를 만들어준다. 하지만 매개변수를 받는 생성자가 하나라도 있을 시 기본 생성자를 입력을 해주어야 한다.
  • 하지만 @NoArgsConstructor 에너테이션이 있을 시 기본 생성자 입력을 하지 않아도 자동으로 스프링에서 해당 에너테이션을 보고 기본 에너테이션을 생성해준다.
  • calendar 생성 시 매개변수로 이름과 설명을 받아야하고 메서드를 호출한 시점의 시간을 createdAt에 담는다.
  • update 매서드 실행시 이름과 설명을 받고, 메서드를 호출한 시점의 시간을 updatedAt에 담는다.
profile
꼬질이

0개의 댓글