Page 1 of 1

About Question enthuware.ocpjad.v7.2.49 :

Posted: Fri Jun 17, 2022 12:05 am
by jogg429
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) {
...
}

Re: About Question enthuware.ocpjad.v7.2.49 :

Posted: Mon Jun 20, 2022 12:08 am
by admin
Required doesn't mean a new transaction is started. It means a new transaction is started if a transaction is not already ongoing. If the transaction has already started, then the same transaction is joined.

You are right. The line EntityManager entityManager; should be added.
thank you for your feedback!