gpt4 book ai didi

java - spring中通过Service访问Join Tables的元素

转载 作者:行者123 更新时间:2023-11-30 05:29:42 25 4
gpt4 key购买 nike

我正在尝试学习它,但遇到了一些概念问题。

让我们想象一下,员工、项目和部门的实体位于:

Employee - Department is a ManyToOne relationship
Employee - Project is a ManyToMany relationship

现在我写了这个:

员工.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...
}

项目.java

@Entity
public class Project {

@Id @GeneratedValue
private Long id;
private String client;

@ManyToMany(mappedBy="project")
private ArrayList<Employee> employees;

//constructors, getters, setters...
}

部门.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 检索项目信息。

但在这种情况下,我似乎没有利用 ProjectEmployee 之间的表关系,那么拥有它有什么意义呢?

最佳答案

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 表(当时 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;

如果根据您的要求,您的查询依赖于数据透视表(例如员工表),只需声明:

@Autowired
private EmployeeRepository employeeRep;

并执行从员工到项目的联接,以获取特定员工的项目列表。

在 JPQL 中,您可以在 projects 上使用 fetch join 来获取与匹配的 employee 关联的 projects,例如:

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();
}

关于java - spring中通过Service访问Join Tables的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57812791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com