java
[Hibernate] Inheritance Mapping
nari0_0
2024. 11. 7. 17:46
728x90
구분자가 0이거나 0이 아니거나를 구분자로 사용해야하는 경우 @DiscriminatorValue(“not null”)를 사용해 처리한 내용을 작성하려고 합니다.
상속 매핑(Inheritance Mapping)은 상속 구조를 데이터베이스 테이블에 매핑하는 방법을 제공합니다.
@DiscriminatorValue(“not null”) 는 null이 아니면서 지원되지 않는 판별자 값의 경우, 명시적으로 매핑되지 않은 모든 값을 처리 경우 이 주석이 있는 클래스에 매핑됩니다.
예시)
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "targetCount")
public abstract class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
@Entity
@DiscriminatorValue("not null")
public class TargetUserEvent extends Event {
// some code...
}
@Entity
@DiscriminatorValue("0")
public class AllUserEvent extends Event {
// some code...
}
위와 같이 사용해 targetCount 값이 0이 아닌경우 필요한 연관관계 및 컬럼을 사용해 처리할 수 있었습니다.
참고 : https://in.relation.to/2016/07/19/null-andnot-null-discriminator-values/
728x90