@Entity
@Table(name = "item")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int price;
private int stockQuantity;
@ManyToOne
@JoinColumn(name = "order_item_id", referencedColumnName = "id")
private OrderItem orderItem;
public void setOrderItem(OrderItem orderItem) {
if (Objects.nonNull(this.orderItem)) {
this.orderItem.getItems().remove(this);
}
this.orderItem = orderItem;
orderItem.getItems().add(this);
}
}
@Entity
public class Food extends Item {
private String chef;
}
@Entity
public class Car extends Item {
private long power;
}
@Entity
public class Furniture extends Item {
private long width;
private long height;
}
create table Car (power bigint not null, id bigint not null, primary key (id))
create table Food (chef varchar(255), id bigint not null, primary key (id))
create table Furniture (height bigint not null, width bigint not null, id bigint not null, primary key (id))
create table item (DTYPE varchar(31) not null, id bigint not null, price integer not null, stockQuantity integer not null, order_item_id bigint, primary key (id))
alter table Car add constraint FKge7by0el7y9941l8ssptf0hu9 foreign key (id) references item
alter table Food add constraint FK9y4qp56rc3069yucfklyi6p64 foreign key (id) references item
alter table Furniture add constraint FKfva03eyd5mv4v7uyj6ekjda81 foreign key (id) references item
@Entity
@Table(name = "item")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE")
public abstract class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int price;
private int stockQuantity;
@ManyToOne
@JoinColumn(name = "order_item_id", referencedColumnName = "id")
private OrderItem orderItem;
public void setOrderItem(OrderItem orderItem) {
if (Objects.nonNull(this.orderItem)) {
this.orderItem.getItems().remove(this);
}
this.orderItem = orderItem;
orderItem.getItems().add(this);
}
}
@Entity
@DiscriminatorValue("FOOD")
public class Food extends Item{
private String chef;
}
@Entity
@DiscriminatorValue("CAR")
public class Car extends Item{
private long power;
}
@Entity
@DiscriminatorValue("FURNITURE")
public class Furniture extends Item{
private long width;
private long height;
}
create table item (DTYPE varchar(31) not null, id bigint not null, price integer not null, stockQuantity integer not null, power bigint, chef varchar(255), height bigint, width bigint, order_item_id bigint, primary key (id))