Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring引导中的DB字段空问题_Spring Boot - Fatal编程技术网

Spring boot Spring引导中的DB字段空问题

Spring boot Spring引导中的DB字段空问题,spring-boot,Spring Boot,我在springboot中有一个方法,可以从登录的员工那里收集各种数据库日期 private boolean isActiveThisMonth(EmployeeView emp,LocalDate date){ return (emp.getHiringDate().withDayOfMonth(1).minusDays(1).isBefore(date.withDayOfMonth(1)) && (emp.getLeavingDate()==null ||

我在
springboot
中有一个方法,可以从登录的员工那里收集各种数据库日期

private boolean isActiveThisMonth(EmployeeView emp,LocalDate date){
        return (emp.getHiringDate().withDayOfMonth(1).minusDays(1).isBefore(date.withDayOfMonth(1)) && (emp.getLeavingDate()==null ||
                emp.getLeavingDate().plusMonths(1).withDayOfMonth(1).isAfter(date.with(lastDayOfMonth()))));
    }

当前,如果数据库中的
hiringDate
字段为空,则返回错误。如何添加异常,以便在字段到达时不会出现错误?或者,如果最好输入一个日期,例如当前日期,这样就不会出现错误?

有几种方法可以防止这种情况

一,

您可以像这样处理
isActiveThisMonth
中的空值

private boolean isActiveThisMonth(EmployeeView emp,LocalDate date){
    LocalDate hiringDate = emp.getHiringDate() != null ? emp.getHiringDate() : LocalDate.now();
    return (hiringDate.withDayOfMonth(1).minusDays(1).isBefore(date.withDayOfMonth(1)) && (emp.getLeavingDate()==null ||
            emp.getLeavingDate().plusMonths(1).withDayOfMonth(1).isAfter(date.with(lastDayOfMonth()))));
}
  • 您可以确保在创建员工时在实体中设置该值

      @Column(name = "hire_date", columnDefinition = "DATE DEFAULT CURRENT_DATE")
      private jDate startDate;
    

    问题是,如果没有招聘日期,你希望它做什么?你能接受默认值吗?你想让它告诉你它没有找到一个没有抛出错误的日期吗?一旦知道如何处理该案例,您可以设置默认值或捕获并转换错误。如果找不到默认返回当前日期的雇用日期,则不会导致空错误