我的编码方式最终导致类型不匹配,因为我使用 JSP 来分配值。 (编辑:applicant_id
类型不匹配)
用例很简单: 在“邀请申请人”屏幕上,用户应该能够选择时间和日期并提交表单,这会将设置的时间、日期分配给申请人,反之亦然。
Main Controller
@RequestMapping("/manageApplicant/invite")
public String inviteScreen(@RequestParam(value="id") int id, Model theModel) {
theModel.addAttribute("interview", new Interview());
Applicant applicants = mainService.getSpecificApplicant(id);
theModel.addAttribute("applicants", applicants);
return "invite-applicant";
}
Interview class variable declaration
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="applicant_id", nullable=false)
private Applicant applicant_id;
Applicant class variable declaration
@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="interview_id")
private Interview interview_id;
.JSP
<form:form action="/management/recruitment/setInterview" modelAttribute="interview">
<form:hidden path="applicant_id" value="${applicants}"/>
Date<form:input type="DATE" path="interviewDate"/><br>
Time<form:input type="TIME" path="interviewTime"/>
<br>
<input type="submit" value="Invite for interview" id="submitButton" />
</form:form>
MySQL TABLES
CREATE TABLE interviews(
id INT auto_increment NOT NULL, PRIMARY KEY(id),
applicant_id int(11),
interviewDate date,
interviewTime time,
CONSTRAINT `FK_Applicant` FOREIGN KEY (`applicant_id`)
REFERENCES `applicant` (`id`)
);
ALTER TABLE applicant ADD
CONSTRAINT `interviews_ibfk_1` FOREIGN KEY (`interview_id`)
REFERENCES `interviews` (`id`);
Interview DAO implementation
public void saveInterview(Interview theInterview) {
// get current session
Session currentSession = sessionFactory.getCurrentSession();
// save interview
currentSession.save(theInterview);
}
最佳答案
两边都不需要@JoinColumn
和ForeignKey。面试包含 Applicant_id,这应该足够了。如果您希望关联是双向的,您应该告诉 JPA 该字段是通过哪个映射到另一侧的。
@OneToOne(mappedby="applicant_id")
private Interview interview_id;
关于java - 错误: type mismatch - how to persist with hibernate a bi-directional OneToOne mapping using spring-mvc forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530752/