@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn
public abstract class Item {
@Id @GeneratedValue
private Long id;
private String name;
private int price;
}
실행 결과
Hibernate:
create table Movie (
id bigint not null,
actor varchar(255),
director varchar(255),
primary key (id)
)
Hibernate:
create table Book (
id bigint not null,
author varchar(255),
isbn varchar(255),
primary key (id)
)
Hibernate:
create table Album (
id bigint not null,
artist varchar(255),
primary key (id)
)
Hibernate:
create table Item (
price integer not null,
id bigint not null,
DTYPE varchar(31) not null,
name varchar(255),
primary key (id)
)
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn
public abstract class Item {
@Id @GeneratedValue
private Long id;
private String name;
private int price;
}
실행 결과
Hibernate:
create table Item (
price integer not null,
id bigint not null,
DTYPE varchar(31) not null,
actor varchar(255),
artist varchar(255),
author varchar(255),
director varchar(255),
isbn varchar(255),
name varchar(255),
primary key (id)
)
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn
public abstract class Item {
@Id @GeneratedValue
private Long id;
private String name;
private int price;
}
실행 결과
Hibernate:
create table Movie (
price integer not null,
id bigint not null,
actor varchar(255),
director varchar(255),
name varchar(255),
primary key (id)
)
Hibernate:
create table Album (
price integer not null,
id bigint not null,
artist varchar(255),
name varchar(255),
primary key (id)
)
Hibernate:
create table Book (
price integer not null,
id bigint not null,
author varchar(255),
isbn varchar(255),
name varchar(255),
primary key (id)
)
@AttributeOverrides
나 @AttributeOverride
를 사용하고 연관관계를 재정의하려면 @AssociationOverrides
나 @AssociationOverride
를 사용한다.@MappedSuperclass
public abstract class BaseEntity {
@Id @GeneratedValue
private Long id;
private String name;
}
@Entity
public class Member extends BaseEntity {
// ...
}
@Entity
public class Seller extends BaseEntity{
// ...
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn
public abstract class Item extends BaseEntity {
@Id @GeneratedValue
@Column(name = "ITEM_ID")
private Long id;
private String name;
private int price;
private int stockQuantity;
@ManyToMany(mappedBy = "items")
private List<Category> categories = new ArrayList<>();
}
@Entity
public class Album extends Item{
private String artist;
}
@Entity
public class Book extends Item{
private String author;
private String isbn;
}
@Entity
public class Movie extends Item{
private String director;
private String actor;
}
@MappedSuperclass
public abstract class BaseEntity {
private String createdBy;
private LocalDateTime createdDate;
private String modifiedBy;
private LocalDateTime modifiedDate;
}