Java 休眠 - 批量更新从更新返回意外的行数:0 实际行数:0 预期:1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2743130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate - Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
提问by Sujee
I get following hibernate error. I am able to identify the function which causes the issue. Unfortunately there are several DB calls in the function. I am unable to find the line which causes the issue since hibernate flush the session at the end of the transaction. The below mentioned hibernate error looks like a general error. It doesn't even mentioned which Bean causes the issue. Anyone familiar with this hibernate error?
我收到以下休眠错误。我能够确定导致问题的功能。不幸的是,函数中有几个 DB 调用。由于休眠在事务结束时刷新会话,因此我无法找到导致问题的行。下面提到的休眠错误看起来像一个一般错误。它甚至没有提到哪个 Bean 导致了这个问题。有人熟悉这个休眠错误吗?
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransacti
onManager.java:500)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManag
er.java:473)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(Transaction
AspectSupport.java:267)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
采纳答案by beny23
Without code and mappings for your transactions, it'll be next to impossible to investigate the problem.
如果没有交易的代码和映射,调查问题几乎是不可能的。
However, to get a better handle as to what causes the problem, try the following:
但是,要更好地了解导致问题的原因,请尝试以下操作:
- 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.
- 在您的休眠配置中,将 hibernate.show_sql 设置为 true。这应该向您显示已执行并导致问题的 SQL。
- 将 Spring 和 Hibernate 的日志级别设置为 DEBUG,这同样会让您更好地了解哪一行导致了问题。
- 创建一个单元测试来复制问题,而无需在 Spring 中配置事务管理器。这应该让您更好地了解有问题的代码行。
Hope that helps.
希望有帮助。
回答by shreyas
I got the same exception while deleting a record by Id that does not exists at all. So check that record you are updating/Deleting actually exists in DB
我在删除根本不存在的 Id 记录时遇到了同样的异常。因此,请检查您正在更新/删除的记录是否确实存在于数据库中
回答by Julius
I just encountered this problem and found out I was deleting a record and trying to update it afterwards in a Hibernate transaction.
我刚刚遇到这个问题,发现我正在删除一条记录,然后尝试在 Hibernate 事务中更新它。
回答by George Papatheodorou
As Juliussays this happens when an update Occurs on an Object that has its children being deleted. (Probably because there was a need for an update for the whole Father Object and sometimes we prefer to delete the children and re -insert them on the Father (new , old doesnt matter )along with any other updates the father could have on any of its other plain fields)
So ...in order for this to work delete the children (within a Transaction) by calling childrenList.clear()
(Dont loop through the children and delete each one with some childDAO.delete(childrenList.get(i).delete()))
and setting
@OneToMany(cascade = CascadeType.XXX ,orphanRemoval=true)
on the Side of the Father Object. Then update the father (fatherDAO.update(father)). (Repeat for every father object) The result is that children have their link to their father stripped off and then they are being removed as orphans by the framework.
正如朱利叶斯所说,当更新发生在其子项被删除的对象上时,就会发生这种情况。(可能是因为需要更新整个父对象,有时我们更喜欢删除子对象并将它们重新插入父对象(新的、旧的无关紧要)以及父对象可能对任何对象进行的任何其他更新它的其他普通字段)所以......为了使其工作删除子项(在事务中)通过调用childrenList.clear()
(不要循环遍历子项并删除每个子项并使用一些childDAO.delete(childrenList.get(i).delete()))
和设置
@OneToMany(cascade = CascadeType.XXX ,orphanRemoval=true)
在父对象的一侧。然后更新父亲(fatherDAO.update(father))。(对每个父亲对象重复)结果是孩子们与父亲的联系被剥夺,然后他们作为孤儿被框架移除。
回答by Sumit Singh
its happen when you try to delete the same object and then again update the same object use this after delete
当您尝试删除同一个对象然后再次更新同一个对象时会发生这种情况,请在删除后使用它
session.clear();
session.clear();
回答by Mr. TA
This can happen when trigger(s) execute additional DML (data modification) queries which affect the row counts. My solution was to add the following at the top of my trigger:
当触发器执行影响行计数的额外 DML(数据修改)查询时,就会发生这种情况。我的解决方案是在触发器顶部添加以下内容:
SET NOCOUNT ON;
回答by cSn
This happened to me once by accident when I was assigning specific IDs to some objects (testing) and then I was trying to save them in the database. The problem was that in the database there was an specific policy for setting up the IDs of the objects. Just do notassign an ID if you have a policy at Hibernate level.
当我将特定的 ID 分配给某些对象(测试)然后我试图将它们保存在数据库中时,这偶然发生在我身上。问题是在数据库中有一个用于设置对象 ID 的特定策略。只是不,如果你有在休眠水平的策略分配一个ID。
回答by MacDaddy
i got the same problem and i verified this may occur because of Auto increment primary key. To solve this problem do not inset auto increment value with data set. Insert data without the primary key.
我遇到了同样的问题,我验证这可能是由于自动增量主键而发生的。为了解决这个问题,不要在数据集上插入自动增量值。插入没有主键的数据。
回答by Shiv
I encountered this problem where we had one-many relationship.
我遇到了这个问题,我们有一对多的关系。
In the hibernate hbm mapping file for master, for object with set type arrangement, added cascade="save-update"
and it worked fine.
在 master 的 hibernate hbm 映射文件中,对于具有设置类型排列的对象,添加cascade="save-update"
并运行良好。
Without this, by default hibernate tries to update for a non-existent record and by doing so it inserts instead.
没有这个,默认情况下,hibernate 会尝试更新不存在的记录,并通过这样做来插入。
回答by Rēda Biramanē
Solution: In the Hibernate mapping file for the id property, if you use any generator class, for that property you should not set the value explicitly by using a setter method.
解决方案:在 id 属性的 Hibernate 映射文件中,如果您使用任何生成器类,则不应使用 setter 方法显式设置该属性的值。
If you set the value of the Id property explicitly, it will lead the error above. Check this to avoid this error. or It's error show when you mention in the mapping file the field generator="native" or "incremental" and in your DATABASE the table mapped is not auto_incremented Solution: Go to your DATABASE and update your table to set auto_increment
如果显式设置Id属性的值,会导致上面的错误。检查此项以避免此错误。或者当您在映射文件中提到字段生成器=“native”或“incremental”时显示错误,并且在您的数据库中映射的表不是 auto_incremented 解决方案:转到您的数据库并更新您的表以设置 auto_increment