我有这个模型和休眠映射:
@Entity
@Table(name = "candidate")
public class Candidate extends Person {
private Set<Vacancy> vacancies= new HashSet<Vacancy>();
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "candidate_vacancy", joinColumns = @JoinColumn(name = "candidate_id"), inverseJoinColumns = @JoinColumn(name = "vacancy_id"))
public Set<Vacancy> getVacancies() {
return vacancies;
}
public void setVacancies(Set<Vacancy> vacancies) {
this.vacancies = vacancies;
}
}
人类:
@MappedSuperclass
public abstract class Person {
private Integer id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
@Column (name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
@NotEmpty
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
空缺图
@Entity
@Table(name = "vacancy")
@XmlRootElement(name="vacancy")
public class Vacancy {
...
@ManyToMany(mappedBy = "vacancies", fetch = FetchType.EAGER)
public Set<Candidate> getCandidates() {
return candidates;
...
}
}
而且我有这样的HTML形式:
<form action="saveCandidate" method="POST">
name: <input type="text" value="${candidate.name}" name="name" />
<input type="hidden" name="id" value="${candidate.id}">
<input type="submit" value="save changes" />
</form>
我用@controller方法处理表单:
public String saveCandidate(Model model, @ModelAttribute Candidate candidate) {
candidateService.update(candidate);
return "candidateMenu";
}
使用这种方式,我有问题。与candodate相关的职位空缺从我的数据库中删除。我将其修复为:
public String saveCandidate(Model model, @ModelAttribute Candidate candidate) {
Candidate candidate2 = candidateService.findById(candidate.getId());
candidate.setName(candidate2.getName());
candidate.setSurname(candidate2.getSurname());
candidate.setPhone(candidate2.getPhone());
candidate.setResumeUrl(candidate2.getResumeUrl());
candidateService.update(candidate2);
return "candidateMenu";
}
但是我认为这是解决我的问题的非常不好的方法。
附言我知道fetchType.lazy不好,但是我不想替换它。
最佳答案
您需要首先从数据库加载您的实体,然后让spring将请求参数绑定到您的实体属性。
所以下面的事情可能会起作用
@ModelAttribute
public Candidate loadCandidate(@RequestParam(value="id",required=false) Long id) {
Candidate candidate=null;
if(id!=null){
candidate=candidateService.findById(id);
}
else{
candidate=new Candidate();
}
return candidate;
}
@RequestMapping(.......)
public String saveCandidate(Model model, @ModelAttribute Candidate candidate) {
candidateService.update(candidate);
return "candidateMenu";
}
由于
loadCandidate()
方法带有@ModelAttribute
注释,因此loadCandidate()
将在控制器中的任何requestmapping处理程序之前被调用。因此在您的情况下,
loadCandidate()
将在saveCandidate()
之前被调用