我想請問現在若有一個entity長成以下這樣:
@Entity
@Table(name="STAFF")
public class Staff {
@Id
@Column(name="STAFF_ID")
private int staffId;
@Column(name="STAFF_NAME")
private String staffName;
@Column(name="ADDRESS")
private String address;
@Column(name="CUSTOMER_ID")
@OneToMany(cascade=CascadeType.ALL,mappedBy="Staff")
private TreeSet<Customer> customers = new TreeSet<>();
......
}
意思是一個店員可以服務多個客人,所以用OneToMany來表示
那對應的table,關於CUSTOMERS的部分要如何描述呢?
例如
CREATE TABLE staff(
STAFF_ID number(20) NOT NULL,
STAFF_NAME varchar(45) NOT NULL,
ADDRESS varchar(255),
//要如何描述TreeSet<Customer> customers
PRIMARY KEY (STAFF_ID)
);
又如果是多對一的情形,例如多個客人對一個店員:
@Entity
@Table(name="CUSTOMER")
public class Customer {
@Id
@Column(name="CUSTOMER_ID")
int customerId;
@Column(name="NAME")
String name;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="STAFF_ID")
Staff staff;
......
}
那對應的表格又該如何描述呢?
CREATE TABLE customer(
CUSTOMER_ID number(20) NOT NULL,
NAME varchar(45) NOT NULL,
//這邊如何描述staff??
PRIMARY KEY (CUSTOMER_ID)
);