大家,当插入实体数量多到一位时,我有一个问题。问题:我要插入一个新记录:StaffID,类型,原因。但是表记录和员工之间的关系很多,因此我不知道如何在此表中使用> dto dto 的Prop是StaffID或员工员工,我该如何插入。TKS这么多!
代码下面:
员工实体
@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);
}
控制器
在控制器中,我不知道要使用哪个类,形式或DTO
@PostMapping("/create")
public String index(Model model, @ModelAttribute("formRecord") RecordForm recordForm,HttpServletRequest request) {
recordService.create(recordForm);
return "/employee/index";
}
JSP
JSP是模态,当单击一行时,我会在表中获取数据。 在此中,数据包括:StaffID,类型,原因。 错误代码为null sextid。
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
您的员工Pojo缺少StaffID(PK)。另外,如果您已经有一个部门变量,为什么要有部门ID?
您的表格可能具有suffs_id,dections_id,staff_name等,因此,对于许多比一,您只需要添加外国密钥参考,并且在pojos中,您必须确定joincolumn为dections_id。类似于您拥有的记录实体。