一、 表信息
公司表
cId |
cName |
cAdress |
Null |
Null |
Null |
表t_company
员工表
sId |
sName |
sAge |
cId |
Null |
Null |
Null |
Nulls |
表t_staff
公司-员工:一对多关系,一个公司对应多个员工。外键设在多的一方。
二、 实体类
Company类
package dao; import java.util.HashSet; import java.util.Set; public class Company { private Integer cId; private String cName; private String cAdress; private Set<Staff> setStaff = new HashSet<Staff>(); public Integer getcId() { return cId; } public Set<Staff> getSetStaff() { return setStaff; } public void setSetStaff(Set<Staff> setStaff) { this.setStaff = setStaff; } public void setcId(Integer cId) { this.cId = cId; } public String getcName() { return cName; } public void setcName(String cName) { this.cName = cName; } public String getcAdress() { return cAdress; } public void setcAdress(String cAdress) { this.cAdress = cAdress; } }
Staff类
package dao; public class Staff { private Integer sId; private String sName; private Integer sAge; private Company company; public Company getCompany() { return company; } public void setCompany(Company company) { this.company = company; } public Integer getsId() { return sId; } public void setsId(Integer sId) { this.sId = sId; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public Integer getsAge() { return sAge; } public void setsAge(Integer sAge) { this.sAge = sAge; } }
Hibernate要求使用集合表示多的数据,用Set集合表示
三、 配置文件
映射配置文件:
Company.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="dao.Company" table="t_company"> <id name="cId" column="cId"><generator class="native"></generator></id> <property name="cName" column="cName"></property> <property name="cAdress" column="cAdress"></property> <set name="setStaff"> <key column="cId"></key> <one-to-many class="dao.Staff"/> </set> </class> </hibernate-mapping>
Staff.hbml.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="dao.Staff" table="t_staff"> <id name="sId" column="sId"><generator class="native"></generator></id> <property name="sName" column="sName"></property> <property name="sAge" column="sAge"></property> <many-to-one name="company" class="dao.Company" column="cId"></many-to-one> </class> </hibernate-mapping>
核心配置文件:
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="dao/Company.hbm.xml"/> <mapping resource="dao/Staff.hbm.xml"/> </session-factory> </hibernate-configuration>
在一对多的一方配置<set>
在多对一的乙方配置<many-to-one>