现实中有很多场景需要用到多对一或者一对多,比如上面这两个类图所展现出来的,一般情况下,一个部门会有多名员工,一名员工只在一个部门任职。
多对一关联映射
在上面的场景中,对于Employee来说,它跟Department的关系就是多对一。
先写实体类
Employee.java
package entity; public class Employee { public int id; public String name; public Department department; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } }