大家好,将实体多对一插入时出现问题。 问题:我要插入新记录:staffId,类型,原因。但是表记录和雇员之间存在多对一的关系,因此我不知道如何使用 DTO类的道具在表中插入staffId或Employee雇员,我该如何插入。非常感谢!
以下代码:
员工实体
@Entity
@Table(name = "STAFF")
public class Employee {
@Column(name = "DEPARTMENT_ID")
private Long departmentId;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(insertable = false, updatable = false)
private Department department;
记录实体
@Entity
@Table(name = "Records", catalog = "Assignment")
public class Records implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private Employee Employee;
private boolean type;
private String reason;
private Date date;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "StaffId", nullable = false)
public Employee getEmployee() {
return this.Employee;
RecordDTO \
在控制器中,我不知道要使用哪个类,表格或DTO 公共类RecordDto {
/*=====================================================================================================
*===== PRIVATE PROPERTIES =====
*=====================================================================================================*/
/**
* trunglq_department.ID
*/
private Long id;
/**
* trunglq_department.Employee
*/
private Employee Employee;
/**
* trunglq_department.type
*/
private boolean type;
/**
* trunglq_department.reason
*/
private String reason;
/**
* trunglq_department.date
*/
private Date date;
RecordForm
public class RecordForm {
/*=====================================================================================================
*===== PRIVATE PROPERTIES =====
*=====================================================================================================*/
/**
* trunglq_department.ID
*/
private Long id;
/**
* trunglq_department.Employee
*/
private int staffId;
/**
* trunglq_department.type
*/
private boolean type;
/**
* trunglq_department.reason
*/
private String reason;
/**
* trunglq_department.date
*/
private Date date;
存储库
@Override
public Long insert(Records record) {
record.setDate(new Date());
return (Long)super.insert(record);
}
服务
@Override
public Long create(RecordForm recordForm) {
recordForm.setStaffId(9);
Records record = (Records) DataTransformUtil.transform(recordForm, Records.class);
return (Long)recordRepository.insert(record);
}
控制器
在控制器中,我不知道要使用哪个类,Form或DTO
@PostMapping("/create")
public String index(Model model, @ModelAttribute("formRecord") RecordForm recordForm,HttpServletRequest request) {
recordService.create(recordForm);
return "/employee/index";
}
JSP
jsp是模态的,单击行时我得到表中的数据。 在此,数据包括:staffId,类型,原因。 错误代码为空StaffId。
form:form modelAttribute="formRecord" action="record/create"
method="POST">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">
Ghi nhận nhân viên có ID:
<label class="idStaff"
style="font-size: 20px"></label>
</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<form:hidden path="staffId" class="idStaff"/>
<div class="custom-control custom-radio custom-control-inline">
<form:radiobutton path="type" class="custom-control-input"
id="customRadio" name="radioRecord" value="0" />
<label class="custom-control-label" for="customRadio">Achievement</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<form:radiobutton path="type" class="custom-control-input"
id="customRadio2" name="radioRecord" value="1" />
<label class="custom-control-label" for="customRadio2">Mistake</label>
</div>
<div class="form-group">
<label for="reason">Reason:</label>
<form:textarea class="form-control" rows="5" id="comment"
path="reason" />
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="submit" class="btn btn-success">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form:form>
错误代码
java.sql.SQLIntegrityConstraintViolationException: Column 'StaffId' cannot be null
答案 0 :(得分:0)
您的员工pojo缺少staffId(PK)。另外,如果已经有Department变量,为什么还要有Department ID?
您的表可能具有staff_id,department_id,staff_name等,因此对于多对一,您只需要添加外键引用,在pojos中,您必须将联接列指定为department_id。类似于您拥有的Record实体。