About Question enthuware.ocpjad.v7.2.49 :
Posted: Fri Jun 17, 2022 12:05 am
Each method (findEmployee, updateEmployee) uses the default transaction mode REQUIRED. So a transaction starts at the beginning of each method and ends at the end of the method.
The employee instance is therefore in DETACHED mode after leaving findEmployee.
So in updateEmployee the employee instance
entityManager.merge(emp);
is needed to get the instance back in MANAGED state.
Because the transaction ends when leaving updateEmployee,the employee instance is persisted into the database automatically.
So in my opinion, the correct answer should be the first one. You only need the code
entityManager.merge(emp);
--------------------------------------------------
There also seems to be missing one line of code in the code sample:
@PersistenceContext(unitName="HRApp-PU")
EntityManager entityManager; // This line is missing
private Employee findEmployee(String badgeld) {
...
}
The employee instance is therefore in DETACHED mode after leaving findEmployee.
So in updateEmployee the employee instance
entityManager.merge(emp);
is needed to get the instance back in MANAGED state.
Because the transaction ends when leaving updateEmployee,the employee instance is persisted into the database automatically.
So in my opinion, the correct answer should be the first one. You only need the code
entityManager.merge(emp);
--------------------------------------------------
There also seems to be missing one line of code in the code sample:
@PersistenceContext(unitName="HRApp-PU")
EntityManager entityManager; // This line is missing
private Employee findEmployee(String badgeld) {
...
}