Skip to content

Commit

Permalink
✨ feat: AuditingEntityListener을 통해 음식 생성 시간 자동 생성 (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Jan 6, 2024
1 parent ac2d1ba commit 94a194d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/diareat/diareat/DiareatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import javax.annotation.PostConstruct;
import java.util.TimeZone;

@EnableCaching
@EnableJpaAuditing
@SpringBootApplication
public class DiareatApplication {

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/diareat/diareat/food/domain/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDate;
Expand All @@ -13,6 +15,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Food {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -33,6 +36,7 @@ public class Food {

private LocalDate date;

@CreatedDate
@Column(name = "added_time") //테이블과 매핑
private LocalDateTime addedTime; //클라이언트에서 추가하도록 요청 보낸 timestamp

Expand All @@ -44,7 +48,6 @@ public static Food createFood(String name, User user, BaseNutrition baseNutritio
food.name = name;
food.user = user;
food.date = LocalDate.of(year, month, day);
food.addedTime = LocalDateTime.now();
food.baseNutrition = baseNutrition;

return food;
Expand Down

0 comments on commit 94a194d

Please sign in to comment.