gpt4 book ai didi

java - 更新时拥有实体实例不再引用具有级联 ="all-delete-orphan"的集合

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:18 27 4
gpt4 key购买 nike

ProjectRequirement 实体之间存在一对多关系。

HTML:

<div class="container">
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<form action="#" th:action="@{/projects/updateProject/(id=${project.id})}" method="post">
<input hidden="hidden" name="id" th:value="${project.id}" />
<div class="form-group">
<label>Project</label>
<input type="text" name="projectNaam" class="form-control" id="projectName" th:value="${project.projectName}" placeholder="Project" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

</div>
<div class="col-sm-2"></div>
</div>
</div>

这是我的 Project Controller 代码:

@RequestMapping(value = "/updateProject", method = RequestMethod.POST)
public String updateProject @ModelAttribute("project") Project project){
this.projectService.saveProject(project);

return "redirect:/projects";
}

这是我的 Project 类:

@Entity
@Table(name = "Project")
public class Project {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String projectName;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "project_id")
private Set<Requirement> requirements;

public Project(){

}

public Project(String projectName) {
this.projectName = projectName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getProjectName() {
return projectName;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}

public Set<Requirement> getRequirements(){ return requirements; }

public void setRequirements(Set<Requirement> requirements){ this.requirements = requirements; }

更新项目时出现错误(仅更新名称)。已经在 Internet 上寻找解决方案,但没有找到适合我的解决方案。

最佳答案

我会尝试以下一项或全部:

  • 您正在通过 Project来自事务上下文之外的对象。确保在保存前将其合并。
  • 确保每个 Requirement在集合中引用了 Project实体。
  • 确保合并后不使用public void setRequirements(Set<Requirement> requirements)方法。
  • 因为您正在使用 Set .确保 Requirement已正确实现hashCodeequals .

关于java - 更新时拥有实体实例不再引用具有级联 ="all-delete-orphan"的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401823/

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