The method in the DAO is responsible for the update (checked, works correctly).

public boolean update(EmployeeEntity employeeEntityUpdate) { Session session = sessionFactory.openSession(); try { session.beginTransaction(); Query query = session.createQuery("update EmployeeEntity set id =:id, name =:name, surname =:surname, age = :age, employedDate =:employedDate, login =:login, password = :password, role= :role"); query.setParameter("name", employeeEntityUpdate.getName()); query.setParameter("surname", employeeEntityUpdate.getSurname()); query.setParameter("age", employeeEntityUpdate.getAge()); query.setParameter("employedDate", employeeEntityUpdate.getEmployedDate()); query.setParameter("login", employeeEntityUpdate.getLogin()); query.setParameter("password", employeeEntityUpdate.getPassword()); query.setParameter("role", employeeEntityUpdate.getRole()); query.setParameter("id", employeeEntityUpdate.getId()); query.executeUpdate(); session.getTransaction().commit(); session.close(); return true; } catch (HibernateException exc) { session.getTransaction().rollback(); return false; } } 

A screenshot of the window where one of the users is selected from the list (always when selecting only the topmost one, the system will update it and enter the relevant information into the database

)

Here employeeEntity is declared static and it is this field that is passed to the onActionUpdate method, that is, when you press the Update key further.

Method when clicking the "Edit" button

  @FXML void onActionEdit(ActionEvent event) throws IOException { mainController = new MainController(); employeeEntity = tableView.getSelectionModel().getSelectedItem(); if (employeeEntity == null) { mainController.newScene("/javafx/employeeNotChosenError.fxml"); } else { FXMLLoader loader = new FXMLLoader(getClass().getResource("/javafx/windowEdit.fxml")); Parent root = loader.load(); Controller4 controller4 = loader.getController(); controller4.setNameEdit(employeeEntity.getName()); controller4.setSurnameEdit(employeeEntity.getSurname()); controller4.setAgeEdit(employeeEntity.getAge()); controller4.setDatePicker(employeeEntity.getEmployedDate()); controller4.setRole(employeeEntity.getRole().toString()); btnEdit.getScene().setRoot(root); } } 

After selecting a user and clicking "Edit": enter image description here

Description of the Update method itself:

  @FXML void onActionUpdate(ActionEvent event) throws IOException { EmployeeService servicex = new EmployeeServiceImpl(); EmployeeEntity employeeEntity = Controller2.employeeEntity; System.out.println(employeeEntity.toString()); employeeEntity.setName(txtFieldName.getText()); employeeEntity.setSurname(txtFieldSurname.getText()); employeeEntity.setAge(Integer.parseInt(txtFieldAge.getText())); Date date = Date.from(datePicker.getValue().atStartOfDay(ZoneId.systemDefault()).toInstant()); employeeEntity.setEmployedDate(date); employeeEntity.setRole(Role.valueOf(roleSt.getValue().toString())); servicex.update(Controller2.employeeEntity); Stage stage = (Stage) btnUpdate.getScene().getWindow(); stage.close(); MainController.mainController.changeScene("/javafx/window2.fxml"); } 

Error after processing the code: 2017 12:50:34 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Duplicate entry 41 for PRIMARY ), however, the value of the first one changes in the database and in the table, the others do not.

  • you also have the wrong architecture. read about the MVC pattern and implement it. - Mikhail Vaysman

1 answer 1

Here is the code

 Query query = session.createQuery("update EmployeeEntity set id =:id, name =:name, surname =:surname, age = :age, employedDate =:employedDate, login =:login, password = :password, role= :role"); query.setParameter("name", employeeEntityUpdate.getName()); query.setParameter("surname", employeeEntityUpdate.getSurname()); query.setParameter("age", employeeEntityUpdate.getAge()); query.setParameter("employedDate", employeeEntityUpdate.getEmployedDate()); query.setParameter("login", employeeEntityUpdate.getLogin()); query.setParameter("password", employeeEntityUpdate.getPassword()); query.setParameter("role", employeeEntityUpdate.getRole()); query.setParameter("id", employeeEntityUpdate.getId()); 

Change to

 Query query = session.createQuery("update EmployeeEntity set name =:name, surname =:surname, age = :age, employedDate =:employedDate, login =:login, password = :password, role= :role where id = :id"); query.setParameter("name", employeeEntityUpdate.getName()); query.setParameter("surname", employeeEntityUpdate.getSurname()); query.setParameter("age", employeeEntityUpdate.getAge()); query.setParameter("employedDate", employeeEntityUpdate.getEmployedDate()); query.setParameter("login", employeeEntityUpdate.getLogin()); query.setParameter("password", employeeEntityUpdate.getPassword()); query.setParameter("role", employeeEntityUpdate.getRole()); query.setParameter("id", employeeEntityUpdate.getId()); 

You do not need to update the key field - you must select a record for it. And then your code is trying to update all the records in the database, and not just the one you need.