简体   繁体   中英

A collection with cascade=“all-delete-orphan” was no longer referenced by the owning entity instance when updating

I have a one to many relationship between Project and Requirement entities.

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>

This is my controller code of Project :

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

    return "redirect:/projects";
}

And this is my Project class:

@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; }

I get the error when updating the project (only updating the name). Already looked on the Internet for a solution, but didn't find one working for me.

I would try one or all of the following:

  • You are passing the Project object from outside of transactional context. Make sure it is merged before saving.
  • Make sure that each Requirement in the Set has reference to Project entity.
  • Make sure that after merge, you do not use public void setRequirements(Set<Requirement> requirements) method.
  • As you are using Set . Make sure that Requirement has properly implemented hashCode and equals .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM