删除mysql hiberhate表中的行

de90aj5v  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(219)

删除表中的行delete(id)-id高亮显示红色(…无法应用于(javafx.scene.control.tablecolumn)我要从数据库表中删除行。

//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();
    }
xghobddn

xghobddn1#

ProductEntity selectedItem = tableView_tableAll.getSelectionModel().getSelectedItem();
delete(selectedItem.getId());

或通过标识符删除,并通过实体立即删除

private void delete(ProductEntity entity)
...
ProductEntity selectedItem = tableView_tableAll.getSelectionModel().getSelectedItem();
delete(selectedItem);

相关问题