About Question enthuware.ocpjad.v7.2.49 :

Post Reply
jogg429
Posts: 2
Joined: Thu Jun 16, 2022 11:46 pm
Contact:

About Question enthuware.ocpjad.v7.2.49 :

Post 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) {
...
}

admin
Site Admin
Posts: 10034
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest