我是Hibernate的新手。 现在从web开始研究这个例子。 我有一个包含一些表的MySQL数据库,包括table" profession"。该表有两个cols:profession_id(自动增量)和profession_name。
在java项目中,我有一个'测试' class,包含几种表处理方法和' main'方法。 处理方法是:
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();
}
&#39;主&#39;方法包含以下操作:
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);
}
所以,问题是:当我运行它时,我得到了
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)
在stackoverflow上阅读了关于这个主题的几个主题后,我了解到,这个问题不知何故与ids有关。所以我决定评论这段代码:
test.deleteEntity(result.get(0));
之后一切正常。
所以问题是: 如果需要,如何避免此异常: 1.删除其中一行(&#39;当我删除例如result.get(3)等时也会出现此问题)。 2.删除剩余的行。
答案 0 :(得分:2)
删除特定Profession
后,也会从ArrayList
中删除该内容。例如
test.deleteEntity(result.get(0));
result.remove(0);
这可以解决您的问题。