通过服务访问 spring 中的 Join Tables 的元素
Access elements of Join Tables in spring through Service
我正在尝试学习它,但遇到了一些概念性问题。
让我们假设有员工、项目和部门的实体,其中:
Employee - Department is a ManyToOne relationship
Employee - Project is a ManyToMany relationship
现在我写了这个:
Employee.java
@实体
public class Employee {
@Id @GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name="department_id")
private String department;
@ManyToMany
@JoinTable(name="project_employee",
joinColumns=@JoinColumn(name="project_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="employee_id", referencedColumnName="id"))
private ArrayList<Project> projects;
//constructors, getters, setters...
}
Project.java
@Entity
public class Project {
@Id @GeneratedValue
private Long id;
private String client;
@ManyToMany(mappedBy="project")
private ArrayList<Employee> employees;
//constructors, getters, setters...
}
Department.java
@Entity
public class Department {
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy="department")
private ArrayList<Employee> employees;
//constructors, getters, setters...
}
EmployeeService.java
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRep;
@Autowired
private ProjectRepository projectRep;
@Autowired
private DepartmentRepository deptRep;
public List<Project> getProjectsByEmployee(Long emplId){
}
}
我想到的第一个实现该方法的解决方案是:
- 通过
employeeRep
获取特定的员工对象,然后遍历其列表项目以检索 ID 并使用它们来检索 projectRep
的项目信息。
但在这种情况下,我似乎没有利用 Project
和 Employee
之间的 table 关系,那么拥有它有什么意义呢?
But in that case it seems to me that I am not exploiting the table
relation I have between Project and Employee, so what's the point on
having it then?
事实上,当您从 Employee
实体访问 Project
关系时,您实际上利用了 ManyToMany table(当时 JPA 为您执行查询)但您没有不要有效地利用它。我会解释。
The first solution that came in my mind to implement that methods was:
Get the specific employee object through the employeeRep and then loop
over its list project retrieving ids and using them to retrieve
project info with projectRep.
这是个坏主意,因为您不想执行很多查询,而一个相当简单的查询就可以实现同样的事情。
同样,按实体定义存储库不是强制性的:
@Autowired
private EmployeeRepository employeeRep;
@Autowired
private ProjectRepository projectRep;
@Autowired
private DepartmentRepository deptRep;
如果根据您的要求,您的查询依赖于数据透视表 table(例如员工 table),只需声明:
@Autowired
private EmployeeRepository employeeRep;
并执行从员工到项目的联接以获取特定员工的项目列表。
在 JPQL 中,您可以在 projects
上使用 fetch join 将 projects
关联到匹配的 employee
,例如:
public List<Project> getProjectsByEmployee(Long empId){
String jpql = "SELECT emp FROM Employee e" +
"INNER JOIN FETCH e.projects" +
"WHERE emp.id=:empId";
TypedQuery<Employee> query = em.createQuery(jpql, Employee.class)
.setParameter("empId", empId);
Employee emp = query.getSingleResult();
return emp.getProjects();
}
我正在尝试学习它,但遇到了一些概念性问题。
让我们假设有员工、项目和部门的实体,其中:
Employee - Department is a ManyToOne relationship
Employee - Project is a ManyToMany relationship
现在我写了这个:
Employee.java @实体
public class Employee {
@Id @GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name="department_id")
private String department;
@ManyToMany
@JoinTable(name="project_employee",
joinColumns=@JoinColumn(name="project_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="employee_id", referencedColumnName="id"))
private ArrayList<Project> projects;
//constructors, getters, setters...
}
Project.java
@Entity
public class Project {
@Id @GeneratedValue
private Long id;
private String client;
@ManyToMany(mappedBy="project")
private ArrayList<Employee> employees;
//constructors, getters, setters...
}
Department.java
@Entity
public class Department {
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy="department")
private ArrayList<Employee> employees;
//constructors, getters, setters...
}
EmployeeService.java
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRep;
@Autowired
private ProjectRepository projectRep;
@Autowired
private DepartmentRepository deptRep;
public List<Project> getProjectsByEmployee(Long emplId){
}
}
我想到的第一个实现该方法的解决方案是:
- 通过
employeeRep
获取特定的员工对象,然后遍历其列表项目以检索 ID 并使用它们来检索projectRep
的项目信息。
但在这种情况下,我似乎没有利用 Project
和 Employee
之间的 table 关系,那么拥有它有什么意义呢?
But in that case it seems to me that I am not exploiting the table relation I have between Project and Employee, so what's the point on having it then?
事实上,当您从 Employee
实体访问 Project
关系时,您实际上利用了 ManyToMany table(当时 JPA 为您执行查询)但您没有不要有效地利用它。我会解释。
The first solution that came in my mind to implement that methods was:
Get the specific employee object through the employeeRep and then loop over its list project retrieving ids and using them to retrieve project info with projectRep.
这是个坏主意,因为您不想执行很多查询,而一个相当简单的查询就可以实现同样的事情。
同样,按实体定义存储库不是强制性的:
@Autowired
private EmployeeRepository employeeRep;
@Autowired
private ProjectRepository projectRep;
@Autowired
private DepartmentRepository deptRep;
如果根据您的要求,您的查询依赖于数据透视表 table(例如员工 table),只需声明:
@Autowired
private EmployeeRepository employeeRep;
并执行从员工到项目的联接以获取特定员工的项目列表。
在 JPQL 中,您可以在 projects
上使用 fetch join 将 projects
关联到匹配的 employee
,例如:
public List<Project> getProjectsByEmployee(Long empId){
String jpql = "SELECT emp FROM Employee e" +
"INNER JOIN FETCH e.projects" +
"WHERE emp.id=:empId";
TypedQuery<Employee> query = em.createQuery(jpql, Employee.class)
.setParameter("empId", empId);
Employee emp = query.getSingleResult();
return emp.getProjects();
}