Reputation: 585
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.
Upvotes: 3
Views: 3108
Reputation: 26572
I would try one or all of the following:
Project
object from outside of transactional context. Make sure it is merged before saving.Requirement
in the Set has reference to Project
entity.public void setRequirements(Set<Requirement> requirements)
method.Set
. Make sure that Requirement
has properly implemented hashCode
and equals
.Upvotes: 3