我的编码方式最终因我使用JSP分配值而导致类型不匹配。 (编辑:applicant_id上的类型不匹配)

用例很简单:
 在“邀请申请人”屏幕上,用户应该能够选择时间和日期,然后提交表格,该表格会将设置的时间,日期分配给申请人,反之亦然。


  主控制器


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



  访谈类变量声明


@OneToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="applicant_id", nullable=false)
private Applicant applicant_id;



  申请人类别变量声明


@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表


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`);



  面试DAO实施


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;

09-05 15:56