Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Enumerated - EnumType.STRING #83

Closed
yoon-youngjin opened this issue Apr 25, 2022 · 1 comment
Closed

@Enumerated - EnumType.STRING #83

yoon-youngjin opened this issue Apr 25, 2022 · 1 comment

Comments

@yoon-youngjin
Copy link
Collaborator

@Enumerated을 이용할 때 반드시 EnumType은 String을 사용하자!

  • @Enumerated 애너테이션에는 두 가지 EnumType이 존재
    • EnumType.ORDINAL: enum 순서 값을 DB에 저장
    • EnumType.STRING: enum 이름을 DB에 저장
enum OrderStatus{
    READY, FINISH
        }
  • 이와 같이 OrderStatus의 enum타입이 명시된 경우라고 가정하자.
@Enumerated(EnumType.ORDINAL)
private OrderStatus orderstatus;
  • EnumType.ORDINAL인 경우 DB에 READY는 1, FINISH는 2인 상태로 저장
@Enumerated(EnumType.STRING)
private OrderStatus orderstatus;
  • EnumType.STRING인 경우 DB에 READY, FINISH로 저장

  • 만약 OrderStatus에 필드가 추가 된다면?

enum OrderStatus{
    READY, DELIVERY,  FINISH
        }
  • EnumType.ORDINAL인 경우에 기존 DB의 OrderStatus 속성값의 2는 DELEVERY가 되어 큰 문제가 발생
  • @Enumerated의 기본 값은 EnumType.ORDINAL이므로 반드시 EnumType.STRING으로 사용하자!
@yoon-youngjin yoon-youngjin changed the title @Enumerated 어노테이션을 이용할 때 반드시 EnumType은 String을 사용하자! @Enumerated 어노테이션을 이용할 때 반드시 EnumType은 String을 사용 Apr 25, 2022
@yoon-youngjin yoon-youngjin changed the title @Enumerated 어노테이션을 이용할 때 반드시 EnumType은 String을 사용 @Enumerated을 이용할 때 반드시 EnumType은 String을 사용 Apr 25, 2022
@yoon-youngjin yoon-youngjin changed the title @Enumerated을 이용할 때 반드시 EnumType은 String을 사용 @Enumerated - EnumType.STRING Apr 25, 2022
@myway00
Copy link
Contributor

myway00 commented Apr 26, 2022

  • EnumType.STRING인 경우 DB에 READY, FINISH로 저장
  • 만약 OrderStatus에 필드가 추가 된다면?
enum OrderStatus{
    READY, DELIVERY,  FINISH
        }
  • EnumType.ORDINAL인 경우에 기존 DB의 OrderStatus 속성값의 2는 DELEVERY가 되어 큰 문제가 발생
  • @Enumerated의 기본 값은 EnumType.ORDINAL이므로 반드시 EnumType.STRING으로 사용하자!
  • FINISH랑 DELIVERY 둘다 똑같은 OrderStatus 속성값을 가지게 되니 나중에 속성값으로 enum 값을 빼올 때 엄청난 에러가 나겠군요..! 항상 Enum 사용할 때는 @Enumerated(EnumType.STRING) 으로 해야겠습니당~~ 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants