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) {
...
}
About Question enthuware.ocpjad.v7.2.49 :
-
- Posts: 2
- Joined: Thu Jun 16, 2022 11:46 pm
- Contact:
-
- Site Admin
- Posts: 10322
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjad.v7.2.49 :
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!
You are right. The line EntityManager entityManager; should be added.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 6 guests