0

I am trying to delete data from a History table and also want to remove the data from the historyList .

If I use the below code i get ConcurrentModificationException .

If I use iterator inside foreach I get IllegalStateException .

I have a History class as an Entity with hid as @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)

if i remove the historyList.remove(d) from code i get org.hibernate.StaleStateException:

Is there any way to overcome this problem . Please help if possible.

Thanks

 for (History d : historyList) {

        Transaction txn = session.beginTransaction();
        session.delete(d);
        historyList.remove(d);
        System.out.println("Deleted Successfully");
        txn.commit();

    }
SSP
  • 2,650
  • 5
  • 31
  • 49

1 Answers1

-1

If you're using an iterator, don't use a for loop

Iterator it = historyList.iterator();
while (it.hasNext()) { 
History h = it.next() ;
Transaction txn = session.beginTransaction(); session.delete(h);
it.remove();
System.out.println("Deleted Successfully"); txn.commit(); }
G3or6311
  • 66
  • 5