当前位置: 首页 > 知识库问答 >
问题:

Hibernate:插入多对一:错误列不能为null

秋阳旭
2023-03-14

各位,我在将实体多对一插入时遇到了一个问题。问题:我想插入一个新记录:staffId,type,reason。但是表记录和employee有多对一的关系,所以我不知道如何在这个表中插入类为DTO is staffId或employee 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;

记录到\

在controller中,我不知道使用哪个类,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;

记录表格

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是模态的,当单击一行时,我在表中获取数据。其中,数据包括:员工ID、类型、原因。错误代码为空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">&times;</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

共有1个答案

司徒高寒
2023-03-14

您的员工pojo缺少staffId(PK)。还有,如果已经有了部门变量,为什么还要有部门id?

您的表可能有staff\u id、department\u id、staff\u name等,因此对于多对一,您只需添加外键引用,在pojos中,您必须将joincolumn指定为department\u id。类似于您的记录实体。

 类似资料:
  • 尊敬的开发者们。 我要问你一个问题,我觉得很难为我解决,只是因为我不知道为什么要回答,我的意思是什么??让我看看。JBoss文档中的关联没有得到全面的答案:为什么我应该使用JoinTable而不是外键,并且我不完全理解映射是如何工作的,我这样说是什么意思?我知道什么是协会是ManyToMany或ManyToOne等,他们的目的是什么,但他们如何工作和协作彼此不需要回答关于双向或单向或联合或协会,我

  • 我正在使用与共享主键一对一映射。

  • 初始情况:我有这两个实体,杂货清单和产品。 杂货清单:一个杂货清单可以有几种产品。 产品:一个产品可以分配给多个杂货店 实体和关系存储在Postgres中的三个不同表中(一个用于列表,一个用于产品,一个用于关系映射表)。 映射表:映射表"lists_products" 示例产品:Id=4的示例产品 问题是:我正在尝试创建新的列表,这很有效。但是,正如您可以从映射表图像中看到的那样,Hibernat

  • 我定义了2个具有单向一对多关系的实体: 在command.class中: 在StockDetails.class中: null

  • 现在我有了hibernate的工作批插入(“hibernate.jdbc.batch_size=50”),但据我所知,hibernate会批量生成单个插入。我知道我可以告诉我的db驱动程序为每批插入创建多行插入,以使用rewriteBatchedStatements:jdbc:p加快性能ostgresql://localhost:5432/mydb?rewriteBatchedStatements

  • 我已经尝试了表2的hibernate映射: 上面不起作用。在获取记录时,它试图从表2中获取表1中根本不存在的LOB代码