Skip to content
woojin.jang edited this page Apr 30, 2026 · 1 revision

Embedded Type

  • 새로운 값 타입을 직접 정의해서 사용할 수 있는데 JPA에서 이것을 임베디드 타입이라고 한다.

Requirements

  • Github Release 테이블 설계

Design

create table github_release
(
    tech_stack   varchar(100)                  not null,
    tag_name     varchar(100)                  not null,
    name         varchar(500)                  null,
    body         mediumtext                    null,
    published_at datetime(6)                   not null,
    prerelease   tinyint(1)  default 0         not null,
    draft        tinyint(1)  default 0         not null,
    status       varchar(20) default 'PENDING' not null,
    primary key (tech_stack, tag_name)
);

1. 복합키의 객체지향적 캡슐화 (@EmbeddedId)

  • 데이터베이스의 복합 PK는 JPA 엔티티 내에서 각각의 필드로 존재할 때보다 하나의 식별자 객체로 묶었을 때 더 강력한 논리를 갖게 된다.
  • 식별자의 단일성 : 엔티티를 식별하기 위해 반드시 이 두 정보가 같이 필요하다는 비즈니스 규칙을 명시적으로 표현할 수 있게 된다.
  • 재사용성 : 다른 테이블에서 이 복합키를 FK로 참조해야 하는 경우가 발생한다면 동일한 복합 PK를 그대로 재사용하여 연관관계를 매핑할 수 있다.

2. 도메인 모델의 응집도 향상

  • 테이블 컬럼 상으로 평면적으로 나열되어 있지만 비즈니스 로직상으로 의미가 밀접한 필드들을 그룹화할 수 있다.
  • 상태정보 분리
  • 코드 가독성 확보

3. JPA의 식별자 관리 규칙 준수

  • Serializable 인터페이스 구현 : 공식문서를 보면 다음과 같이 명시가 되어있다.
  • The following rules apply for composite primary keys:
    • The primary key class must be public and must have a public no-arg constructor.
    • The access type (field- or property-based access) of a primary key class is determined by the access type of the entity for which it is the primary key unless the primary key is a embedded id and a different access type is specified. See Section Section 2.3.
    • If property-based access is used, the properties of the primary key class must be public or protected.
    • The primary key class must be serializable.
    • The primary key class must define equals and hashCode methods. The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.
    • A composite primary key must either be represented and mapped as an embeddable class (see Section 11.1.17) or must be represented as an id class and mapped to multiple fields or properties of the entity class (see Section 11.1.22).
    • If the composite primary key class is represented as an id class, the names of primary key fields or properties in the primary key class and those of the entity class to which the id class is mapped must correspond and their types must be the same.
    • A primary key that corresponds to a derived identity must conform to the rules of Section 2.4.1.
  • JPA 식별자 클래스는 반드시 Serializable을 구현해야 하며, equalshashCode를 재정의해야 한다.
  • 임베디드 타입을 사용하면 복합키 관리 로직을 엔티티 외부로 격리하게 된다.
  • 뿐만 아니라 영속성 컨텍스트는 @Id를 통해 엔티티를 관리한다. 복합키인 경우 객체 자체를 Key로 사용하기 때문에, 임베디드 타입을 사용하면 em.find()로 조회가 가능하다.

📖 Java

📖 Kotlin

📖 Coroutine

📖 Spring

📖 Spring Security

📖 Spring Batch

📖 Reactive Programming

📖 Database

📖 MySQL

📖 Redis

📖 JPA

📖 QueryDsl

📖 MSA

📖 Kafka

📖 Apache Flink

  • [Apache Flink - Apache Flink Architecture]
  • [Apache Flink - Stream Processing]
  • [Apache Flink - Data Stream API & Window]
  • [Apache Flink - State Management]

📖 HTTP

📖 AWS

📖 Docker

📖 Kubernetes

📖 CI/CD

📖 Nginx

📖 Monitoring🥈

  • [Monitoring - Log Concept]
  • [Monitoring - Log Level & Filter]
  • [Monitoring - Logback]
  • [Monitoring - Log Collection with ELK Stack]
  • [Monitoring - Log Monitoring with Kibana]
  • [Monitoring - Building a Monitoring System with Spring Boot Actuator]
  • [Monitoring - Server Monitoring with Prometheus and Grafana with Discord Alerts]

📖 Test

📖 Effective Java 3/E

📖 Kotlin Academy - Effective Kotlin

📖 Kotlin Academy - 핵심편

📖 스프링으로 시작하는 리액티브 프로그래밍

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 1

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 2

📖 Clean Code

📖 리팩토링 2판

📖 주니어 백엔드 개발자가 반드시 알아야 할 실무 지식

📖 GraphQL

Clone this wiki locally