gpt4 book ai didi

java - Spring,在一个查询中向现有的多对多列表添加一列

转载 作者:行者123 更新时间:2023-12-02 01:11:07 24 4
gpt4 key购买 nike

我有一个 Project 实体,它具有以下属性:

  @ManyToMany
@JoinTable(
name = "employee_projects",
joinColumns = @JoinColumn(name = "project_id"),
inverseJoinColumns = @JoinColumn(name = "employee_id")
)
private List<Employee> employees;

当我向 employees 添加另一个 employee 时,我正在执行以下服务方法。

  Employee addEmployeeToProject(long id, Employee employee) {
Project project = findByProjectId(id);
List<Employee> oldEmployees = project.getEmployees();
oldEmployees.add(employee);
project.setEmployees(oldEmployees);
projectRepository.save(project);
return employee;
}

效果很好。但后来我实际上开始记录 SQL 请求,这就是实际发生的情况:

2019-12-17 18:45:52.644 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL                        : delete from employee_projects where project_id=?
2019-12-17 18:45:52.646 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.648 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.648 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.650 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.650 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.651 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.651 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.651 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.652 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.652 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.652 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.652 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.653 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.653 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)
2019-12-17 18:45:52.653 DEBUG 14324 --- [nio-8080-exec-1] org.hibernate.SQL : insert into employee_projects (project_id, employee_id) values (?, ?)

所以他首先删除所有列(从给定项目中删除所有员工)。然后再次添加所有以前的+新的。我想如果我更换整个集合就会发生这种情况。但我只需要插入一个对象,这样为了节省时间,我可以编写自己的插入查询,这样它只插入我想要插入的一名新员工,而不是删除所有内容并再次添加所有内容。但是,除了编写自己的查询之外,还有更好的方法在一个请求/查询中完成此操作吗?

编辑:

如果我使用 native 查询,我可以只定义 INSERT 查询吗? JPQL 中不可能吗?

最佳答案

当然。首先,首先在单个事务中完成所有这些操作,从而受益于对托管实体的修改自动持久化这一事实。无需调用 save()

然后,了解 Java 传递对象的引用。如果您获得项目员工的集合,您不会获得该集合的副本。您会得到它的引用。因此,将员工添加到该集合中会有效地修改该项目的员工集合。无需在项目中再次设置集合。

最后,你所需要的就是

@Transactional
public void addEmployeeToProject(long id, Employee employee) {
Project project = findByProjectId(id);
project.getEmployees().add(employee);
}

关于java - Spring,在一个查询中向现有的多对多列表添加一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59379642/

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