inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

JPA & Spring Data JPA 기초

상속 관계 엔티티 로직을 분기처리 하는 법?

380

skehdxhd

작성한 질문수 14

0

안녕하세요 상속관계 엔티티를 다루는 방법에 대해 궁금한 점 있어 질문 드립니다

상속관계의 엔티티가 존재하고, 자식 엔티티마다 서비스 로직이 다를 수 있을 때 어떻게 분기처리하는지 알고 싶어 질문 드립니다

 

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "base_type")
public abstract class BaseEntity {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    private String description;

    @Column(name = "base_type", updatable = false, insertable = false, length = 30)
    private String baseType;

    protected BaseEntity(Long id, String title, String description) {
        this.id = id;
        this.title = title;
        this.description = description;
    }
}

@Entity
@DiscriminatorValue(value = "FIRST")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class FirstEntity extends BaseEntity{

    private int like;

    @Builder
    public FirstEntity(Long id, String title, String description, int like) {
        super(id, title, description);
        this.like = like;
    }
}        

@Entity
@DiscriminatorValue(value = "SECOND")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class SecondEntity extends BaseEntity {

    private String redirectUrl;

    @Builder
    public SecondEntity(Long id, String title, String description, String redirectUrl) {
        super(id, title, description);
        this.redirectUrl = redirectUrl;
    }
}


이러한 상속구조 엔티티가 있고, 두 자식의 서비스 로직이 다를 수 있을 때
컨트롤러에서 분기처리를 어떻게 하는지 알고 싶습니다

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/test")
public class TestController {

    private final TestService testService;

    @GetMapping
    public void findEntity(@RequestParam String type) {

        if(type.equalsIgnoreCase("first")) {

        } else if(type.equalsIgnoreCase("second")) {
            
        }
        //...
        //...
        //...
    }
}

만약 이러한 구조로 간다면, 자식클래스가 늘어났을 때 또 if문을 추가해야해서 효율적이지 못할 것 같아서요!

지금 생각한 것은 상태 패턴을 이용하면 어떨까 하는데, 이게 좋은지 잘 모르겠습니다..

public interface BaseContext {

    boolean support(String type);

    void executeFind();
}

이렇게 인터페이스를 만들어주고

@Component
public class FirstContext implements BaseContext{

    private static final String TYPE = "FIRST";
    private final FirstService firstService;

    public FirstContext(FirstService firstService) {
        this.firstService = firstService;
    }

    @Override
    public boolean support(String type) {
        return TYPE.equalsIgnoreCase(type);
    }

    @Override
    public void executeFind() {
        firstService.find();
    }
}

자식클래스의 상태를 구현하는 클래스를 만들어주고

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/test")
public class TestController {
    private final List<BaseContext> contexts;

    @GetMapping
    public void findEntity(@RequestParam String type) {

        BaseContext findContext = contexts.stream()
                .filter(context -> context.support(type))
                .findAny()
                .orElseThrow(() -> new IllegalStateException("Error"));
        
        findContext.executeFind();
    }
}

컨트롤러에서는 이렇게 받아주면

자식 엔티티가 늘어나도 if-else를 하지 않아도 된다고 생각했습니다

다만, 자식 엔티티가 추가될 때 마다 Context를 추가해야 한다는 점과

Context가 Service를 바라보고있어 Service가 수정되면 Context와 Service 2가지 모두 수정해야한다는 점이 마음에 걸립니다

보통 상속관계의 엔티티를 다룰 때 어떤식으로 분기처리를 하나요??

강사님이 아니시더라도 다른 수강생분들의 의견도 달아주시면 좋겠습니다!!

spring jpa

답변 1

0

최범균

이럴 땐 아래와 같이 각 상위 엔티티 타입에 추상 메서드를 정의하고, 각 하위 엔티티 타입에서 서로 다른 로직을 구현해 보면 어떨까요?

public abstract class BaseEntity {
    ...

    public abstract void someMethod(); // 하위 타입마다 다르게 동작해야 하는 기능
}

public class FirstEntity extends BaseEntity {
    ...
    @Override
    public void someMethod() {
        // FirstEntity에 맞게 구현
    }
}

네이버 API | '검색 - 책' API 제공 중단

0

5

0

Part 2 듣기전 복습하던 중, 아키텍처에 관한 질문이 있습니다.

0

22

2

장치 의존성과 CSR

0

22

1

테스트는 인터페이스를 대상으로 만드는것에 대해 궁금한 점이 있습니다.

0

11

1

[영한님]Spring Boot 및 프로젝트 생성

0

37

2

리뉴얼 시기

0

29

2

JPA 엔티티로 만들었다고 기술종속이 아닌지에 대한 질문

0

46

3

강의 요청 문의

0

37

2

클로드코드 명령어-대화&세션관리 수업 자료

0

37

1

PlatformTransactionManager, ResourcelessTransactionManager 질문

1

56

2

자료 한번에 다운받기

0

29

1

스프링 빈 등록 방법 2가지 다 알아야하는 이유

0

53

1

mybatis 를 이용한 crud에서 insert에서 resultType='long" 이라고 하셨는데요,

0

24

1

설계 트레이드 오프 링크 접속 안됨

0

82

2

테스트 질문, 어떤것을 사용할것인것에 대해서

0

40

1

깃허브주소 궁금합니다.

0

35

2

mariaDB 설정

0

29

1

git 링크 안들어가져요 404에러

0

493

1

JoinColumn 질문

0

470

1

1:N 매핑과 해당 방식 차이가 뭔가요?

0

542

2

10강 3:37 질문

0

344

1

7강에서 SecondaryTable을 왜 사용하지 모르겠습니다.

0

668

1

JPA 실무 @Entity을 @Embeddable 대체하는 경우가 많은가요?

0

566

1

도메인과 JPA 엔티티

0

830

1