JPA Annotations

Seongmin·2023년 8월 22일

JPA

목록 보기
1/1

@Inheritance

클래스의 상속 구조와 데이터베이스 테이블 간의 상속 구조를 매핑

import javax.persistence.*;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Vehicle {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    // 공통 필드 및 메서드
}

@Entity
public class Car extends Vehicle {
    private String model;
    
    // Car 클래스의 필드 및 메서드
}

@Entity
public class Truck extends Vehicle {
    private double payloadCapacity;
    
    // Truck 클래스의 필드 및 메서드
}

strategy

InheritanceType.JOINED

  • 각 클래스마다 별도의 테이블을 생성하지만, 부모 클래스와 서브 클래스 간의 연결이 유지됩니다. 서브 클래스 테이블은 부모 클래스 테이블과 조인하여 필요한 정보를 얻습니다.

InheritanceType.SINGLE_TABLE

  • 하나의 테이블로 클래스 계층 구조를 매핑합니다. 부모 클래스와 서브 클래스 모두 하나의 데이터베이스 테이블에 저장됩니다.

InheritanceType.TABLE_PER_CLASS

  • 각 클래스마다 별도의 테이블을 생성하여 매핑합니다. 부모 클래스와 서브 클래스가 각각 별도의 테이블을 가집니다.
  • 서브 클래스마다 중복된 데이터가 발생할 수 있어 데이터 중복 문제가 발생할 수 있습니다.

@EnableJpaRepositories

JPA repository들을 활성화하여 빈으로 등록한다. 기본적으로 @Configuration가 붙은 클래스의 하위 패키지를 스캔한다.

TO BE CONTINUED...

출처


@Inherited
https://codeung.tistory.com/259

0개의 댓글