Reputation: 1
I'm using JPA entities from database, and trying to create, modify or delete registers in database through EntityManager.
I have a 'Company' entity, and this entity has a Collection of 'Employee' entity.
When I modify, create or remove some register in the Employee table, the Collection of 'Employee' from the Company entity is not updated, only when I stop and run the project again (I'm using glassFish server)
However, if I print the whole list of employees directly using the findAll() method using the Employee entity facade, the employees are shown updated.
Here is the code:
public class test extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@EJB
CompanyFacade companyDB;
@EJB
EmployeeFacade employeeDB;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
Company company = companyDB.find("Z-5849729");
//CREATE
if(request.getParameter("create")!=null){
Employee employee = new Employee(3);
employee.setBirthdate("05/06/1998");
employee.setCompanyrfc(company);
employee.setSalary(1600);
employee.setSector("Client Support");
employee.setName("Alfonso Guerrero");
employeeDB.create(employee);
}
//REMOVE
if(request.getParameter("remove")!=null){
Employee employee = employeeDB.find(3);
employeeDB.remove(employee);
}
//EDIT
if (request.getParameter("edit")!=null){
Employee employee = employeeDB.find(3);
employee.setName("Alfonso Modified");
employeeDB.edit(employee);
}
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet test</title>");
out.println("</head>");
out.println("<body>");
out.println(company.getCompanyrfc()+" |" + company.getName());
Address address = company.getZipcode();
List<Employee> employees = (List<Employee>) company.getEmployeeCollection();
for (Employee employee : employees){
System.out.println(employee.getName());
out.println(employee.getName());
}
List<Items> items = ( List<Items>) company.getItemsCollection();
for (Items item : items){
out.println(item.getItemname());
}
List<Employee> employeeList = employeeDB.findAll();
for (Employee employee : employeeList){
out.println("<p>"+employee.getName()+"</p>");
}
out.println(" |" + address.getCountry() + " " + address.getAdminname());
out.println("<h1>Servlet test at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
}
When I use the company.getEmployeeCollection() from the Company Entity, I get the list of employees not updated.
If I use the employeeDB.findAll() from the Employee Entity Facade, I get the list of employees updated.
Upvotes: 0
Views: 532
Reputation: 4434
I suspect that your EntityManager flush mode type is set to COMMIT and not AUTO. When the flush mode type is set to COMMIT, the flush action, that synchronizes the persistence context to the underlying database occurs at transaction commit.
public static final FlushModeType COMMIT
Flushing to occur at transaction commit. The provider may flush at other times, but is not required to.
If you want the synchronization to the underlying database to occure at execution of the query, set the flush mode type to AUTO
public static final FlushModeType AUTO
(Default) Flushing to occur at query execution.
Alternatively you could directly call the flush()
operation to explicitely require the synchronization with the underlying DB right after your query.
flush()
Synchronize the persistence context to the underlying database.
Upvotes: 1