java - 错误: type mismatch - how to persist with hibernate a bi-directional OneToOne mapping using spring-mvc forms

标签 java mysql spring hibernate spring-mvc

我的编码方式最终导致类型不匹配,因为我使用 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/

相关文章:

java - 使用 NamedParamterJdbcTemplate 对象指定要查询的 mysql 字段和值

java - 我想使用一种在部署到服务器后只调用一次的方法

mysql - 比 MySQL 中的 find_in_set 更快的方法(逗号分隔的长整型)

php - GroupBy 和 OrderBy Laravel Eloquent

php - 更改数据库中的记录

java - Spring jpa中如何指定主键可以为空

java - 无法访问从 FTL 文件中的 modelmap 发送的对象列表

java - 关闭 JFrame 时如何在后台运行应用程序?

java - Vaadin Sampler 中的 `ComponentRenderer` 是什么类?

java - 如何在高分辨率屏幕上正确缩放图像