以下代碼是大型項目的一部分。java - 是否直接分配函數調用變量(沒有new())引發異常?
概述是,我試圖訪問使用Spring MVC的數據庫。我想根據請求更新一個字段,併發送有關數據庫發回的值的響應。
代碼:
@Override
@Transactional
public EmployeeResponse update(EmployeeRequest employeeRequest) {
Employee emp = new Employee();
UUID empId = UUID.fromString(employeeRequest.getId());
Employee foundEmployee = employeeRepository.findOne(empId);
if (foundEmployee != null) {
foundEmployee.setAddress(employeeRequest.getAddress());
// similarly set 4 fields of foundEmployee
emp = employeeRepository.save(foundEmployee);
}
EmployeeResponse response = new EmployeeResponse();
response.setAddress(emp.getAddress());
// similarly set 4 fields of response
return response;
}
我發現,沒有new Employee()
爲foundEmployee
,因爲是emp
。 我不確定,但我認爲這會導致異常。 我正確嗎?
另外,請告訴我什麼例外,我應該拋出foundEmployee
是null
。
其他信息 - 這是幫助說明:
org.springframework.data.repository.CrudRepository
public T findOne(ID id)
Retrieves an entity by its id.
Parameters:
id - must not be null.
Returns:
the entity with the given id or null if none found
Throws:
IllegalArgumentException - if id is null
什麼是錯誤引發? – iMBMT
「* foundEmployee沒有新的Employee()*」=>不清楚。你能詳細說明嗎? – assylias
這只是一個參考,沒有像'Car c = new Car()'這樣的新關鍵字。 – cst1992