์• ์ •์ฝ”๋”ฉ ๐Ÿ’ป

Project/DARLING 2021.12.03 ๋Œ“๊ธ€ 0๊ฐœ Joana

2. Entity

package so.ego.re_darling.domains.coupon.domain;

import lombok.*;

import javax.persistence.*;
import java.time.LocalDateTime;

@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "coupon")
@Entity
public class Coupon {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NonNull private String title;
  private String content;
  private LocalDateTime useDate;

  @Enumerated(value = EnumType.STRING)
  private CouponStatus status;

  @Builder
  public Coupon(@NonNull String title, String content, LocalDateTime useDate, CouponStatus status) {
    this.title = title;
    this.content = content;
    this.useDate = useDate;
    this.status = status;
  }
}

- Entity class์˜ ํ•„๋“œ์˜ ์ ‘๊ทผ์ œ์–ด์ž๋Š” ์•ˆ์ „ํ•˜๊ฒŒ private ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

- Entity๋Š” DB ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” Column๋“ค์„ ํ•„๋“œ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด์ด๋‹ค. ( DB์˜ ํ…Œ์ด๋ธ”๊ณผ 1๋Œ€1๋กœ ๋Œ€์‘๋œ๋‹ค. )

 

1. @AllArgsConstructor

- NotNull์ด๋‚˜ final ์ด ๋ถ™์€ ํ•„๋“œ๋„ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

- ํด๋ž˜์Šค์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•œ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค.

- @NonNull ์ด ์žˆ๋‹ค๋ฉด ์ƒ์„ฑ์ž ๋‚ด์—์„œ null-check ๋กœ์ง์„ ์ž๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค.

 

2. @NoArgsConstructor

- ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

- Entity ๋Š” ๋ฐ˜๋“œ์‹œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ณ  public์ด๋‚˜ protected์—ฌ์•ผ ํ•œ๋‹ค. (access ๊ฑธ์–ด์ค€ ์ด์œ )

- JpaRepository์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

 

3. @NonNull

- NotNull ์ธ ํ•„๋“œ์— SQL ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋‚ด๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ ์šฉ

 

4. @Enumerated

- EnumType.STRING ์„ ์‚ฌ์šฉํ•˜๋ฉด enum ํƒ€์ž… ๊ทธ๋Œ€๋กœ DB์— ์ €์žฅ๋œ๋‹ค.

- EnumType.ORDINAL ์„ ์‚ฌ์šฉํ•˜๋ฉด enum ํƒ€์ž… ์˜ ์ˆœ์„œ๋ฅผ DB์— ์ €์žฅํ•œ๋‹ค. (๋น„์ถ”)

 

5. @Builder

Setter๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด Entity์˜ ์ธ์Šคํ„ด์Šค ๊ฐ’๋“ค์ด ์–ธ์ œ ์–ด๋””์„œ ๋ณ€๊ฒฝ๋˜๋Š”์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์—†๋‹ค.

๊ทธ๋ ‡๋‹ค๊ณ  ํ•ด์„œ ์ƒ์„ฑ์ž๋กœ ํ•„๋“œ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š”๊ฒƒ ๋˜ํ•œ ์œ„ํ—˜ํ•˜๋‹ค (ํ˜„์žฌ ๋„ฃ๋Š” ๊ฐ’์ด ์–ด๋–ค ํ•„๋“œ์˜ ๊ฐ’์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.)

๋”ฐ๋ผ์„œ Builder ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

ํ•„๋“œ๊ฐ€ ๋งŽ์•„์ง€๋”๋ผ๋„ ์–ด๋–ค ๊ฐ’์„ ์–ด๋–ค ํ•„๋“œ์— ๋„ฃ๋Š”์ง€ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜์ • ํ•˜๋ฉด์„œ
์ง€์†์ ์œผ๋กœ ์ถ”๊ฐ€/์ˆ˜์ • ๋ฐœ์ƒํ•  ์˜ˆ์ •!

 

๋ฐ˜์‘ํ˜•

'Project > DARLING' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

7. CI/CD ๊ณ„ํš  (0) 2022.02.09
6. ๋ฌด์—‡์ด ๋ฌธ์ œ์ผ๊นŒ ...  (0) 2022.01.25
5. Spring REST Docs ์‚ฌ์šฉ  (0) 2022.01.24
4. ์—ฐ๊ด€๊ด€๊ณ„ - OneToMany  (0) 2022.01.23
3. ์—ฐ๊ด€๊ด€๊ณ„ - ManyToOne  (0) 2022.01.23
2. Entity  (0) 2021.12.03