Java 当DB中的列为null时的if语句
伙计们,我正在制作一个网格库,它使用hibernate和SpringMVC从db读取数据。。我有两个表employee,其中有EPMID、EMPNAME、EMPAGE、SALARY、ADDRESS、department\u id(在department表中引用department\u id)和department\u id和name。。。这是Department.javaJava 当DB中的列为null时的if语句,java,spring,hibernate,Java,Spring,Hibernate,伙计们,我正在制作一个网格库,它使用hibernate和SpringMVC从db读取数据。。我有两个表employee,其中有EPMID、EMPNAME、EMPAGE、SALARY、ADDRESS、department\u id(在department表中引用department\u id)和department\u id和name。。。这是Department.java public class Department { @Id @GeneratedValue(strategy = Gener
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "department_id")
private int depId;
@Column(name = "name")
private String depName;
@OneToMany(mappedBy="department",cascade = { CascadeType.ALL }, orphanRemoval=true)
private List<Employee> employees;}
以及在服务中添加员工的功能
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void addEmployee(String[] list, Employee employee) {
employee.setEmpName(list[2]);
employee.setEmpAge(Integer.parseInt(list[4]));
employee.setEmpAddress(list[6]);
employee.setSalary(list[1]);
//employee.getDepartment().setDepId(Integer.parseInt(list[3]));
Department dept = departmentDao.getDepartment(Integer.parseInt(list[3]));
if(dept.equals(null)){
employee.setDepartment(departmentDao.getDepartment(16));
}
employee.setDepartment(dept);
this.employeeDao.addOrEditEmployee(employee);
}
但在输入部门id时,在父部门id中找不到。。出现空指针异常。。我想将department_id设置为16,而不是默认情况下Java中的每个类都将
Java.lang.Object
作为其超类。由于对象类提供了equals
方法,因此您可以在dept
对象上调用它
if (dept.equals(null)) {
employee.setDepartment(departmentDao.getDepartment(16));
}
null在Java中不是一个对象。因此,if语句应该是:
if(dept == null) {
\* ---- your code goes here --- *\
}
默认情况下,Java中的每个类都将
Java.lang.Object
作为其超类。由于对象类提供了equals
方法,因此您可以在dept
对象上调用它
if (dept.equals(null)) {
employee.setDepartment(departmentDao.getDepartment(16));
}
null在Java中不是一个对象。因此,if语句应该是:
if(dept == null) {
\* ---- your code goes here --- *\
}