我有这个模型和休眠映射:
@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";
}
使用这种方式,我有问题。从我的数据库中删除与候选日期相关的空缺。我这样修复它:
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";
}
但我认为这是解决我的问题的非常糟糕的方法。
PS我知道 fetchType.lazy 不好,但我不想替换它。