JoinTable을 사용하여 Notification이라는 테이블을 분리하고자 하였습니다.

graph TD
  Notification --> CommentNotification
  Notification --> BoardNotification
  Notification --> ReportNotification

그래서 코드를 아래와 같이 작성하였습니다.

반복되는 코드는 생략하였습니다.

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DiscriminatorColumn(name = "notificationType", discriminatorType = DiscriminatorType.STRING)
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
public abstract class Notification extends BaseEntity {
    ...
}

@Getter
@NoArgsConstructor
@DiscriminatorValue(NotificationType.Values.COMMENT)
@Entity
public class CommentNotification extends Notification {
    ...
}

@DiscriminatorValue(NotificationType.Values.REPORT)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
public class ReportNotification extends Notification {
    ...
}

@DiscriminatorValue(NotificationType.Values.BOARD)
@NoArgsConstructor
@Getter
@Entity
public class BoardNotification extends Notification {
	...
}

문제 발생

Notification 테이블과 그에 대한 하위 조인 테이블들은 잘 생성이 되고 값은 추가되는거 같은데, DiscriminatorColumn은 들어오지 않았습니다.

확인1. DDL

image (1).png

IntelliJ에서 엔티티를 DDL로 바꿔주는 기능 (아마 JPA 버디일거 같습니다…?) 을 활용하여 실제로 제대로 된 테이블을 생성하나 확인을 해보았습니다.

놀랍게도… 아무런 문제가 없었습니다!

확인2. 다형성 활용

M5에서는 Notification마다 각각의 Repository를 생성했지만, AI-DT 프로젝트에서는 NotificationRepository에서만 모든 것을 담당하려고 했습니다.

그렇게 생각한 것은 두 가지 이유가 있습니다.

  1. Abstract로 되어서 상속을 받고 있다.
  2. 복잡한 쿼리는 결국 QueryDSL로 활용할테니 간단한 부분은 문제가 없을 것이다.