2016-04-12 12 views
2

球员我正在做一个网格库,使用休眠和弹簧mvc从数据库读取数据..我有两个表员工有EPMID,EMPNAME,EMPAGE,SALARY,ADDRESS,department_id refrences在部门表),并已和部门标识名称部门部门标识......这里是Department.java如果陈述当数据库中的列为空

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;} 

Employee.java

public class Employee { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "EMPID") 
private int empId; 

@Column(name = "EMPNAME") 
private String empName; 

@Column(name = "ADDRESS") 
private String empAddress; 

@Column(name = "SALARY") 
private String salary; 

@Column(name = "EMPAGE") 
private int empAge; 

@ManyToOne(cascade={CascadeType.ALL}) 
@JoinColumn(name="department_id") 
private Department department;} 

,并且增加了格兰员工在服务

功能
@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); 
} 

但进入DEPARTMENT_ID当父部门标识..一个空指针异常没有发现发生..我想设置的部门标识等于16,而不是

回答

-1

在Java中每个类都有java.lang.Object因为默认情况下它的超类。由于equals方法由Object类提供,因此您可以在dept对象上调用它。

if (dept.equals(null)) { 
    employee.setDepartment(departmentDao.getDepartment(16)); 
} 

而null不是Java中的对象。所以if语句应该是:

if(dept == null) { 
    \* ---- your code goes here --- *\ 
}