删除表mysql hiberhate中的一行

时间:2018-05-12 21:29:01

标签: mysql hibernate javafx

删除表中的行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();
    }

1 个答案:

答案 0 :(得分:1)

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

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

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