Reputation: 141
I'm new to Hibernate. Now working on the example from web. I have a MySQL database that contains some tables, including table "profession". This table has two cols: profession_id(auto-increment) and profession_name.
In java project I have a 'Test' class, that contains several methods for table processing and 'main' method. Processing methods are:
private void addProfession(String name) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Profession r = new Profession();
r.setProfessionName(name);
session.save(r);
session.getTransaction().commit();
}
private ArrayList<Profession> getProfessions() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
ArrayList<Profession> result = (ArrayList<Profession>) session
.createQuery("from Profession order by professionName").list();
session.getTransaction().commit();
return result;
}
private void deleteProfessions(ArrayList<Profession> result) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
for (Profession p : result) {
System.out.println("Delete: " + p.getProfessionId() + ": " + p.getProfessionName());
session.delete(p);
}
session.getTransaction().commit();
}
private void deleteEntity(Object object){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.delete(object);
session.flush();
session.getTransaction().commit();
}
'main' method contains these operations:
public static void main(String[] args) {
Test test = new Test();
test.addProfession("Profession_1");
test.addProfession("Profession_2");
test.addProfession("Profession_3");
test.addProfession("Profession_4");
test.addProfession("Profession_5");
ArrayList<Profession> result = test.getProfessions();
test.deleteEntity(result.get(0));
test.deleteProfessions(result);
}
So, the problems is: when I run it I get
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3197)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3434)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:98)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
at firstproject.Main.deleteProfessions(Main.java:33)
at firstproject.Main.main(Main.java:57)
After reading several topics about this theme at stackoverflow I learned, that this problem somehow is connected with ids. So I decided to comment this code:
test.deleteEntity(result.get(0));
After that everything worked fine.
So the question is: How do I avoid this exception if i need: 1. Delete one of the rows('cos this problem also occurs when I delete for example result.get(3) and so on). 2. Delete remaining rows.
Upvotes: 1
Views: 2188
Reputation: 1065
After deleting a particular Profession
also remove that from the ArrayList
. For example
test.deleteEntity(result.get(0));
result.remove(0);
This should solve your problem.
Upvotes: 2