休眠删除错误:批量更新返回意外的行数 [英] Hibernate Delete Error: Batch Update Returned Unexpected Row Count

查看:146
本文介绍了休眠删除错误:批量更新返回意外的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面写了这个方法,假设从数据库中删除一个成员记录。但是当我在servlet中使用它时,它会返回一个错误。
$ b MemberDao Class

  public static void deleteMember(Member member){
Session hibernateSession = HibernateUtil.getSessionFactory()。getCurrentSession();
Transaction tx = hibernateSession.beginTransaction();
hibernateSession.delete(member);
tx.commit();
}

控制器部分

  if(delete!= null){
HttpSession httpSession = request.getSession();
成员=(成员)httpSession.getAttribute(member);

MemberDao.deleteMember(member);

nextPage =ledenlijst.jsp;
}

HTTP状态500

  org.hibernate.StaleStateException:批处理更新返回来自update [0]的意外行计数;实际行数:0;预计:1 

有时甚至会在我尝试多次执行页面时抛出此错误。 p>

  org.hibernate.exception.GenericJDBCException:无法执行JDBC批量更新

有人知道到底是什么导致了这些错误吗?

解决方案

错误可能是由几件事引起的。我没有为此付出代价,发现它此处 a>。
$ b



  1. 在提交对象之前刷新数据可能会导致清除所有
    对象如果对象具有自动生成的主键并且您是
    则强制分配键

  2. 如果您正在清理将对象提交给
    数据库之前的对象。
  3. 零或不正确的ID:如果将ID设置为零或其他值,
    Hibernate将尝试更新,而不是插入。

  4. 对象是陈旧的:Hibernate从会话中缓存对象。如果
    对象被修改,并且Hibernate不知道它,它将
    抛出这个异常 - 注意StaleStateException


blockquote>

另请参阅此答案 beny23 ,它提供了一些进一步的提示来发现问题。



  • 在你的hibernate配置中,将hibernate.show_sql设置为true。这应该会显示执行的SQL并导致问题。

  • 将Spring和Hibernate的日志级别设置为DEBUG,同样这会让您更好地了解哪条线导致问题。

  • 创建一个单元测试,它可以在不配置Spring中的事务管理器的情况下复制问题。这应该会让您更好地了解有问题的代码行。



I wrote this method below that is suppose to delete a member record from the database. But when I use it in my servlet it returns an error.

MemberDao Class

public static void deleteMember(Member member) {
    Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = hibernateSession.beginTransaction();
    hibernateSession.delete(member);
    tx.commit();
}

Controller Part

if(delete != null) {
    HttpSession httpSession = request.getSession();
    Member member = (Member) httpSession.getAttribute("member");

    MemberDao.deleteMember(member);

    nextPage = "ledenlijst.jsp";
}

HTTP Status 500

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

Sometimes it even throws this error when I try to execute the page multiple times.

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

Does anybody know what exactly is causing these errors?

解决方案

The error can be caused by several things. I'm not taking the credit for it, found it here.

  1. Flushing the data before committing the object may lead to clear all object pending for persist.
  2. If object has primary key which is auto generated and you are forcing an assigned key
  3. if you are cleaning the object before committing the object to database.
  4. Zero or Incorrect ID: If you set the ID to zero or something else, Hibernate will try to update instead of insert.
  5. Object is Stale: Hibernate caches objects from the session. If the object was modified, and Hibernate doesn’t know about it, it will throw this exception — note the StaleStateException

Also look at this answer by beny23 which gives a few further hints to find the problem.

  • In your hibernate configuration, set hibernate.show_sql to true. This should show you the SQL that is executed and causes the problem.
  • Set the log levels for Spring and Hibernate to DEBUG, again this will give you a better idea as to which line causes the problem.
  • Create a unit test which replicates the problem without configuring a transaction manager in Spring. This should give you a better idea of the offending line of code.

这篇关于休眠删除错误:批量更新返回意外的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆