1. 配置查询对象的模型
1.1 E-R图
1.2 模型映射(代码省略get/set方法)
有的关系配置的单向,可根据情况自行配置为双向
1.2.1 部门
多个部门一个项目经理
一个部门多个员工
@Entity
@Table(name = "t_department")
public class Department {
@Id
@GeneratedValue
private Long id;
private String name;
private String provice;
private String city;
private String street;
private String sn;
@ManyToOne(fetch = FetchType.LAZY)
private Employee manager;
@OneToMany(mappedBy = "department")
private Set<Employee> employees = new HashSet<Employee>();
}
1.2.2 员工
多个员工一个部门
@Entity
@Table(name = "t_employee")
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
private BigDecimal salary;
private Date hireDate;
@ManyToOne(fetch = FetchType.LAZY)
private Department department;
}
1.2.3 电话
多个电话属于一个员工
@Entity
@Table(name = "t_phone")
public class Phone {
@Id
@GeneratedValue
private Long id;
private String types;
private String number;
@ManyToOne
private Employee employee;
}
1.2.4 项目
多个项目由一个项目经理负责
项目和员工为多对多关系
@Entity
@Table(name = "t_project")
public class Project {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
private Employee manager;
@ManyToMany(cascade = CascadeType.REMOVE)
@JoinTable(name = "t_project_employee", joinColumns = { @JoinColumn(name = "project_id") }, inverseJoinColumns = {@JoinColumn(name = "employee_id"