Класс сущности EmployeeInfo
@Entity
@Table(name = "employeeinfo")
public class EmployeeInfo{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "employeeId")
private String employeeId;
@Column(name = "firstName")
private String firstName;
@Column(name = "middleName")
private String middleName;
@Column(name = "lastName")
private String lastName;
......
}
Другой класс сущности ProjectTaskComments
@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@Column(name = "comments")
private String comments;
@Basic(optional = false)
@Column(name = "commentTime")
@Temporal(TemporalType.TIMESTAMP)
private Date commentTime;
@Column(name = "fkCommentedBy")
private Long fkCommentedBy;
@Transient
@JsonIgnoreProperties
private EmployeeInfo commentedEmployee;
@Transient
@Autowired
EmployeeInfoService employeeInfoService;
public EmployeeInfo getCommentedEmployee() {
EmployeeInfo employeeInfo;
employeeInfo = employeeInfoService.getSingleEmployeeInfoByFkUserId(this.fkCommentedBy);
if(employeeInfo != null) {
this.commentedEmployee.setEmployeeId(employeeInfo.getEmployeeId());
this.commentedEmployee.setFirstName(employeeInfo.getFirstName());
this.commentedEmployee.setMiddleName(employeeInfo.getMiddleName());
this.commentedEmployee.setLastName(employeeInfo.getLastName());
this.commentedEmployee.setPhoto(employeeInfo.getPhoto());
return commentedEmployee;
} else {
return null;
}
}
}
Я пытался найти объект EmployeeInfo в методе getCommentedEmployee () с помощью свойства fkCommentedByи установите в @Transient свойство commentedEmployee.
Я обнаружил следующие ошибки:
2018-10-11 13:07:56.834 WARN 16756 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[1]->com.activeboss.model.pm.ProjectTasks["projecttaskcommentsCollection"]->org.hibernate.collection.internal.PersistentBag[0]->com.activeboss.model.pm.ProjectTaskComments["commentedEmployee"])
2018-10-11 13:07:56.853 WARN 16756 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[1]->com.activeboss.model.pm.ProjectTasks["projecttaskcommentsCollection"]->org.hibernate.collection.internal.PersistentBag[0]->com.activeboss.model.pm.ProjectTaskComments["commentedEmployee"])
Как мне решить эту проблему?