-1

I have a number of items in a Javafx table. I want to be able to do a multiple row delete

I have added the below code however i need advice regards Hibernate

shiftonTable.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

@FXML
private void handleDeleteShiftOn() {
    int selectItem = shiftonTable.getSelectionModel().getSelectedIndex();
    if (selectItem >= 0) {

         ObservableList<ShiftOnDate> itemsSelected, AllSelected;
         AllSelected = shiftonTable.getItems();  
            itemsSelected = shiftonTable.getSelectionModel().getSelectedItems();

        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.setTitle("Confirmation Dialog");
        alert.setHeaderText("Look, a Confirmation Dialog");
        alert.setContentText("Are you sure you want to Delete?");

        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == ButtonType.OK) {
            rosterService.removeShiftOnDate(itemsSelected);
            loadShiftOn();
        } else


@Override
public void removeShiftOnDate(ObservableList<ShiftOnDate> itemsSelected) {

    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    //ShiftOnDate s = (ShiftOnDate) session.load(ShiftOnDate.class, itemsSelected);
    session.delete(itemsSelected);
    session.getTransaction().commit();

    session.close();
}

I can see the itemsSelected but am unsure how to pass correctly to hibernate to remove the items.

Rod
  • 41
  • 1
  • 5
  • Here's a [tutorial on deleting items with hibernate](https://www.baeldung.com/delete-with-hibernate). Perhaps you can work out what you need to do from that. That uses a entity manager factory, but you are using a hibernate session directly, so maybe not applicable (but maybe it is best to [use an entity manager factory](https://stackoverflow.com/questions/5640778/hibernate-sessionfactory-vs-entitymanagerfactory). Probably all you need to do is iterate over the items in the observablelist, calling `session.delete` for each item. – jewelsea Sep 19 '19 at 23:45
  • I have just iterated over the items in the observablelist. It works fine. I guess I was looking for a different approach. (Maybe I thought there was some cool way of doing it in Hibernate) Couldn't see the trees for the forest. I will continue with this approach . Thanks for pointing out the obvious. Much appreciated. – Rod Sep 20 '19 at 00:30

2 Answers2

0

You should try org.hibernate.ScrollableResults class to delete multiple items. Below is an example of how to delete:

public static void purgeyourRecords() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Criteria cr = session.createCriteria(EntityClass.class);
            cr.add(Restrictions.eq("fieldName", fieldValue));
            ScrollableResults items = cr.scroll();
            int count = 0;
            while (items.next()) {
                EntityClass entity= (EntityClass) items.get(0);
                session.delete(entity);
                if (++count % 100 == 0) {
                    session.flush();
                    session.clear();
                }
            }
            tx.commit();
        } catch (HibernateException asd) {
            log.debug(asd.getMessage());
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            session.close();
        }

    }

This will remove all the records within the Criteria. You can create your List first as cr.list and pass it as a variable as well.

Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
0

Iterate over the list and call delete on each of the items:

@Override
public void removeShiftOnDate(ObservableList<ShiftOnDate> itemsSelected) {    
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();
    for (ShiftOnDate item: itemsSelected) {
        session.delete(item);
    }
    session.getTransaction().commit();    
    session.close();
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406