Reputation: 11
Deleting a row in a table delete(id) - id highlights red (… cannot be applied to (javafx.scene.control.TableColumn) I want to delete a row from the database table.
//delete button
button_del.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEvent -> {
int selectedIndex = tableView_tableAll.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
// tableView_tableAll.getItems().remove(selectedIndex);
delete(id);
} else {
// Nothing selected
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Ошибка");
alert.setHeaderText("No row selected for deletion");
alert.setContentText("Select row in table");
alert.showAndWait();
}
});
}
}
private void delete(Integer id ) {
Session session = utils.HibernateSessionFactory.getSessionFactory().openSession();
session.beginTransaction();
ProductEntity productEntity =(ProductEntity) session.get(ProductEntity.class,id);
session.delete(productEntity);
session.getTransaction().commit();
session.close();
}
Upvotes: 0
Views: 46
Reputation: 11
ProductEntity selectedItem = tableView_tableAll.getSelectionModel().getSelectedItem();
delete(selectedItem.getId());
or remove deletion by the identifier and immediately delete through the entity
private void delete(ProductEntity entity)
...
ProductEntity selectedItem = tableView_tableAll.getSelectionModel().getSelectedItem();
delete(selectedItem);
Upvotes: 1