gpt4 book ai didi

java - hibernate 删除错误 : Batch Update Returned Unexpected Row Count

转载 作者:IT老高 更新时间:2023-10-28 20:44:31 26 4
gpt4 key购买 nike

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

MemberDao 类

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

Controller 部分

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

MemberDao.deleteMember(member);

nextPage = "ledenlijst.jsp";
}

HTTP 状态 500

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

当我尝试多次执行页面时,有时它甚至会抛出此错误。

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

有人知道导致这些错误的确切原因吗?

最佳答案

错误可能是由多种原因引起的。我没有把它归功于它,找到它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

另请查看 this answerbeny23这为找到问题提供了一些进一步的提示。

  • 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.

关于java - hibernate 删除错误 : Batch Update Returned Unexpected Row Count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com