<hibernate-mapping package="com.itcast.f_hbm_oneToMany"> <class name="Department" table="department"> <!--generator主键生成策略 --> <id name="id"> <generator class="native" /> </id> <property name="name" /> <set name="employees"> <key column="departmentID"></key> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
由于上面这个映射文件里我employees少写了s,造成了错误。映射文件里的属性是跟实体Department里的属性是一一对应的。实体Department如下:
public class Department { private Integer id; private String name; private Set<Employee> employees = new HashSet<Employee>(); // 关联的很多员工 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } @Override public String toString() { return "[Department: id=" + id + ", name=" + name + "]"; } }