TABLE customer(id int, name varcher,PRIMARY KEY id) TABLE address(id int, address_name varcher, zipcode varcher, city varcher,customer_id int, PRIMARY KEY id)
表address中的customer_id为customer表中的id的值,通过customer_id可以找到相应的customer的记录。 将表customer映射为CustomerEO实体,代码如下: @Entity @Table(name="customer") public class CustomerEO implement Serializable{ @Id @GeneratedValue(stragegy=GenerationType=AUTO) private int id;
TABLE customer(id int, name varcher,PRIMARY KEY id) TABLE address(id int, address_name varcher, zipcode varcher, city varcher,PRIMARY KEY id)
TABLE ref_customer_address(customer_id int not null, address_id int not null unique) 将表customer映射为CustomerEO实体,代码如下: @Entity @Table(name="customer") public class CustomerEO implement Serializable{ @Id @GeneratedValue(stragegy=GenerationType=AUTO) private int id;
TABLE customer(id int, name varcher,PRIMARY KEY id) TABLE address(id int, address_name varcher, zipcode varcher, city varcher,PRIMARY KEY id)
TABLE ref_customer_address(customer_id int not null, address_id int not null unique) 创建customer_id外键 ALTER TABLE ref_customer_address ADD CONSTRAINT fk_ref_customer FOREIGN KEY (customer_id) REFERENCES customer (id); 创建address_id外键 ALTER TABLE ref_customer_address ADD CONSTRAINT fk_ref_address FOREIGN KEY (address_id) REFERENCES address(id);
映射实体CustomerEO的代码如下: @Entity @Table(name="customer") public class CustomerEO implement Serializable{ @Id @GeneratedValue(stragegy=GenerationType=AUTO) private int id;
@OneToMany public Collection<AddressEO> getAddress() { return address; } .... }