Java 带有cascade=";“全部删除孤立项”;更新时所属实体实例不再引用
我在Java 带有cascade=";“全部删除孤立项”;更新时所属实体实例不再引用,java,spring,hibernate,jpa,thymeleaf,Java,Spring,Hibernate,Jpa,Thymeleaf,我在项目和需求实体之间有一对多的关系 Html: 这是我的项目课程: @Entity @Table(name = "Project") public class Project { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String projectName; @OneToMany(fetch = FetchType.EAGER,
项目
和需求
实体之间有一对多的关系
Html:
这是我的项目
课程:
@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; }
@实体
@表(name=“项目”)
公共类项目{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有整数id;
私有字符串projectName;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,orphan=true)
@JoinColumn(name=“project\u id”)
私人设置要求;
公共工程(){
}
公共项目(字符串projectName){
this.projectName=projectName;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getProjectName(){
返回项目名称;
}
public void setProjectName(字符串projectName){
this.projectName=projectName;
}
公共集合getRequirements(){return requirements;}
public void setRequirements(Set requirements){this.requirements=requirements;}
我在更新项目(仅更新名称)时出错。
已在Internet上查找解决方案,但未找到适合我的解决方案。我将尝试以下一种或所有方法:
- 您正在从事务上下文外部传递
对象。请确保在保存之前将其合并项目
- 确保集合中的每个
都引用了需求
实体项目
- 确保合并后,您不会使用
方法public void setRequirements(Set requirements)
- 在使用
。确保设置时
已正确实施要求
和hashCode
等于
@RequestMapping(value = "/updateProject", method = RequestMethod.POST)
public String updateProject @ModelAttribute("project") Project project){
this.projectService.saveProject(project);
return "redirect:/projects";
}
@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; }