[JPA] Serializable 직렬화와 고찰

김진회·2023년 5월 25일
0

jpa

목록 보기
5/5

1. 직렬화란?

자바 시스템 내부에서 사용되는 객체 또는 데이터를 외부의 자바 시스템에서도 사용할 수 있도록 byte 형태로 데이터 변환하는 기술

장점

  • 자바 시스템에서의 개발에 최적화
  • 복잡한 데이터 구조의 객체라도 직렬화 기본 조건만 지키면 바로 직렬화 가능
  • 데이터 타입이 자동으로 맞춰짐

2. 사용법

아래 조건에 맞춰 클래스에 implement Serializable를 한다.

조건

  • 직렬화
    • Serializable 인터페이스를 상속받은 객체
    • 기본형 타입(int, char, String, short, double, long, byte 등)은 직렬화 가능
    • transient가 사용된 멤버는 전송되지 않음
    • 생성자나 메소드는 직렬화 대상에 속하지 않음
  • 역직렬화
    • 직렬화 대상 클래스가 classpath에 존재해야 하며, import 되어야 함
  • 자바 직렬화 대상 객체는 동일한 serialVersionUID 를 가지고 있어야 한다 private static final long serialVersionUID = 1L;

3. JPA에서의 Serializable

사실 이 포스팅을 쓴 이유이다.
JPA 표준스펙은 Entity에서의 Serializable 사용을 지향하지만, 우아한&김영한님의 생각은 다르다. 이유는 아래와 같다.

Q(질문자):
가끔 블로그를 보면 entity에 Serializable을 붙이는 경우가 있는데 이것에 대한 설명 부탁드립니다.
Serializable은 객체를 외부 서버로 보낼 때 객체로는 보낼 수 없어서 스트림으로 만들기 위해 사용하는 것으로 알고 있는데, 보통 dto로 매핑을 받으니 entity가 다른 곳으로 이동할 일이 없다고 보거든요.
오히려 외부 서버에 보내기 위해서는 dto를 serializable로 만드는게 맞지 않을까요?

A(김영한):
결론부터 말씀드리면 Q님이 생각하신게 맞습니다.
JPA 표준 스펙은 Entity에 Serializable를 구현하도록 되어 있습니다. JPA 구현체에 따라 엔티티를 분산 환경에서 사용할 수 있거나, 직열화해서 다른 곳에 전송할 수 있는 가능성을 열어준거지요.

하지만 저희가 주로 사용하는 하이버네이트 구현체는 제가 아는 바로 다음 사례를 제외하고는 Entity를 직열화해서 내부에서 사용하는 경우는 못봤습니다. https://www.inflearn.com/questions/16570

그래서 저는 실용적인 관점에서 실무에서 엔티티에 Serializable를 거의 사용하지 않습니다. (하지만 표준 스펙이니 적용해두는게 더 나은 선택입니다.)


Reference

https://www.inflearn.com/questions/17117
https://techblog.woowahan.com/2550/

profile
SSAFY 7기. HMG. 협업, 소통, 사용자중심

6개의 댓글

comment-user-thumbnail
2024년 8월 13일

Carlo Ancelotti as Real Madrid coach admitted that it was not easy for him to immediately adjust with the new players. However, he believes that the Los Merengues squad can immediately find the ideal performance even in a short time.
https://ru.nobartv.co.id/indeks-topik , https://es.nobartv.co.id/indeks-topik , https://th.nobartv.co.id/indeks-topik , https://fr.nobartv.co.id/indeks-topik
"It's the start of the season and the players haven't had much time to prepare for the game. However, we face it with confidence and belief," said Don Carlo, as quoted from the club's website .
https://www.nobartv.co.id/indeks-topik , https://en.nobartv.co.id/indeks-topik , https://ko.nobartv.co.id/indeks-topik , https://ja.nobartv.co.id/indeks-topik , https://ar.nobartv.co.id/indeks-topik , https://hi.nobartv.co.id/indeks-topik
Real Madrid just underwent their first pre-season 2024/2025 trial, in early August 2024. El Real has devoured a total of 3 trial matches. Two warm-up matches ended in defeat, namely when they met AC Milan and Barcelona. While the last match ended with a victory over Chelsea.

답글 달기
comment-user-thumbnail
2025년 5월 22일

Thank you for sharing. I also have something to share that I would like to tell you Welcome to the definitive NYT strands help tools(strands hints today answers), puzzle enthusiasts!
strands help today

답글 달기
comment-user-thumbnail
2025년 6월 7일

Brainrot Merge: Italian Brainrot
Brainrot Merge is an engaging puzzle game designed to challenge your cognitive abilities. With increasingly difficult levels and a variety of puzzle types, it will keep your brain active and entertained for hours.
Brainrot Merge
Brain Rot Merge

답글 달기
comment-user-thumbnail
2025년 8월 28일

JPA에서 Serializable 사용 여부에 대한 고찰이라니, 정말 흥미로운 주제네요! 직렬화에 대한 고민이 마치 전략 게임 같아요. 표준 스펙과 김영한님 같은 전문가의 의견이 다른 점도 흥미롭고요. 문득 age of war (https://ageofwar.pro)가 생각나네요, 거기서도 시대별 전략이 정말 중요하잖아요!

답글 달기
comment-user-thumbnail
2025년 8월 28일

와, JPA에서 Serializable 사용에 대한 고찰이라니 흥미롭네요! 저도 객체 직렬화 개념이 헷갈릴 때가 많았는데, 명확하게 정리해주셔서 감사합니다. 복잡한 문제들을 해결하는 과정이 마치 Royal Match (https://royalmatch.run/) 퍼즐 풀 때 전략 짜는 것 같달까요? :) 잘 읽었습니다!

답글 달기
comment-user-thumbnail
2025년 8월 29일

JPA에서 Serializable 사용에 대한 고찰이라니, 흥미롭네요! 저도 이 부분 항상 궁금했었는데 덕분에 잘 알아갑니다. 이렇게 복잡한 기술 이야기 듣고 나니 갑자기 집에서 Grow a Garden Recipes (https://growagardencooking.recipes/) 보면서 맛있는 거 만들어보고 싶어지네요!

답글 달기