How to iterate a join column in JPA Entity that has a Null Value on database

Viewed 138

I have a problem in iterating the departments.department_id when it has a null value on the database. when i try to get the departments.department_id.

for (int i = 0; i < listEmp.size(); i++) {
  employeeMap.put("empDeptId" + i, StringUtil.isDataNull(listEmp.get(i).Departments.DEPARTMENT_ID));[enter image description here][1]
}
return employeeMap;

In my employee entity i have a join column of departments

@Entity
@Table(name="EMPLOYEES")
@SequenceGenerator(name = "EMPLOYEES_SEQ", sequenceName = "EMPLOYEES_SEQ")
public class Employees {

  @ManyToOne(optional = true)
  @JoinColumn(name= "DEPARTMENT_ID",nullable = true)
  public Departments Departments;

} 

And Departments entity

@Entity
@Table(name="DEPARTMENTS")
public class Departments {

  public Integer DEPARTMENT_ID;

  public String DEPARTMENT_NAME;

}

UPDATE: Found a work around.

if (listEmp.get(i).Departments == null)
  employeeMap.put("empDeptId" + i, "");
else
  employeeMap.put("empDeptId" + i, StringUtil.getStrFormat(listEmp.get(i).Departments.DEPARTMENT_ID));

But is there a way to solve this with just utilizing the Employees.class?

0 Answers
Related