Fw: [问题] Hibernate的entity与表格的对应方式

楼主: lueichun (no anonymous)   2021-01-10 15:58:53
我想请问现在若有一个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)
);

Links booklink

Contact Us: admin [ a t ] ucptt.com