[英]HIbernate: insert many to one: error Column cannot be null
Ho everyone, i have a problem when insert entity many to one.大家好,我在多对一插入实体时遇到问题。 problem: I wana insert a new record: staffId, type, reason.
问题:我想插入一条新记录:staffId、类型、原因。 But table Records and employee have relationship many to one, so i dont know how to insert in this table with prop of class DTO is staffId or Employee employee, and how can i insert that.
但是表记录和员工有多对一的关系,所以我不知道如何在这个表中插入类 DTO 的道具是员工 ID 或员工员工,我该如何插入。 Tks so much!
太棒了!
code below:代码如下:
employee entity雇员实体
@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;
Records entity记录实体
@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 \\
In controller, i dont know which class to use, Form or DTO public class RecordDto {
在控制器中,我不知道要使用哪个类,Form 或 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;
repository存储库
@Override
public Long insert(Records record) {
record.setDate(new Date());
return (Long)super.insert(record);
}
service服务
@Override
public Long create(RecordForm recordForm) {
recordForm.setStaffId(9);
Records record = (Records) DataTransformUtil.transform(recordForm, Records.class);
return (Long)recordRepository.insert(record);
}
Controller控制器
In controller, i dont know which class to use, Form or DTO
在控制器中,我不知道要使用哪个类,Form 还是 DTO
@PostMapping("/create")
public String index(Model model, @ModelAttribute("formRecord") RecordForm recordForm,HttpServletRequest request) {
recordService.create(recordForm);
return "/employee/index";
}
JSP JSP
jsp is modal, i get data in table when click on a row.
jsp 是模态的,单击一行时我会在表中获取数据。 in this, data include: staffId, type, reason.
其中,数据包括:staffId、类型、原因。 Error code is null 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>
Error code错误代码
java.sql.SQLIntegrityConstraintViolationException: Column 'StaffId' cannot be null
Your staff pojo is missing staffId(PK).您的员工 pojo 缺少员工 ID(PK)。 Also why do you have department id if you already have a Department variable?
另外,如果您已经有一个部门变量,为什么还要有部门 ID?
Your table might have staff_id, department_id, staff_name etc, so for many to one, you just need to add the foreign key reference and in pojos, you have to specific the joincolumn to be department_id.你的表可能有staff_id、department_id、staff_name等,所以对于多对一,你只需要添加外键引用,在pojos中,你必须将joincolumn指定为department_id。 similar to the Record entity you have.
类似于您拥有的 Record 实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.