1部门----多员工 在hibernate中一对多的对象模型关系:看下面部门类的set集合,这里主要是讲hibernate一对多的映射文件xml public class Dept { private int id; private String deptname; //一对多的体现 private Set<Employee> emps; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } public Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } } 在xml文件中需要注意:set集合的配置。它中的dept_id是要和many-to-one相对应的。也就是上一篇BLOG中我们声明的部分。 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.hibernate.model"> <class name="Dept" table="dept"> <id name="id"> <generator class="native"/> </id> <property name="deptname" column="deptname"/> <!--一对多的对象模型关系 --> <set name="emps" > <!-- 和多方的配置文件也就是emp的配置文件中设置的对应dept_id是一样的 --> <key column="dept_id"></key> <!-- one-to-many指定多方是哪个表 --> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping> 在建立了这样的ORM关系映射之后呢。我们再进行相关的CRUD的时候hibernate也就会给我们进行相关自动的CRUD操作。方便了很多代码上的麻烦。