在休眠 saveOrUpdate() 期间获取 org.hibernate.StaleStateException
Posted
技术标签:
【中文标题】在休眠 saveOrUpdate() 期间获取 org.hibernate.StaleStateException【英文标题】:Getting a org.hibernate.StaleStateException during hibernate saveOrUpdate() 【发布时间】:2016-02-25 16:24:28 【问题描述】:我的PostgreSQL
数据库中有以下数据库表:
CREATE TABLE sl_office_template
(
layout_id integer NOT NULL,
description character varying(500),
time_created timestamp without time zone,
time_updated timestamp without time zone,
template_name character varying(65),
site_id integer,
template_page_type integer,
template_theme character varying(10),
template_id bigserial NOT NULL,
office_template_id character varying(10),
CONSTRAINT sl_office_template_template_id PRIMARY KEY (template_id),
CONSTRAINT lay_id_fkey FOREIGN KEY (layout_id)
REFERENCES sl_layout (layout_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
现在,当我尝试保存任何新记录时,出现以下异常:
08:36:38.366 [http-nio-8080-exec-8] ERROR org.hibernate.jdbc.AbstractBatcher - Exception executing batch:
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:85) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2525) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2478) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2805) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:114) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) [hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:662) [spring-orm-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) [spring-tx-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) [spring-tx-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:515) [spring-tx-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) [spring-tx-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at com.sun.proxy.$Proxy44.saveOrUpdateTemplate(Unknown Source) [na:na]
at com.smartwcm.admin.navigation.controller.NavigationController.saveOrUpdateSiteTemplate(NavigationController.java:1762) [NavigationController.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_31]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_31]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [catalina.jar:8.0.18]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.18]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.18]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.18]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.18]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.18]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.18]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) [catalina.jar:8.0.18]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.18]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [catalina.jar:8.0.18]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [catalina.jar:8.0.18]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.18]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) [catalina.jar:8.0.18]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.18]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) [catalina.jar:8.0.18]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) [tomcat-coyote.jar:8.0.18]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) [tomcat-coyote.jar:8.0.18]
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) [tomcat-coyote.jar:8.0.18]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) [tomcat-coyote.jar:8.0.18]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) [tomcat-coyote.jar:8.0.18]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_31]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_31]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.18]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_31]
08:36:38.373 [http-nio-8080-exec-8] ERROR o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session
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:85) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244) ~[hibernate-core-3.5.1-Final.jar:3.5.1-Final]
还有:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:701)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:800)
在 hbm.xml 文件中,我有以下内容:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 29, 2010 1:16:19 PM by Hibernate Tools 3.2.5.Beta -->
<hibernate-mapping>
<class name="com.smartwcm.template.commons.to.OfficeTemplate" table="sl_office_template" catalog="public">
<id name="templateId" type="java.lang.Integer">
<column name="template_id" />
<generator class="increment" />
</id>
<property name="officeTemplateId" type="string">
<column name="office_template_id" />
</property>
<property name="siteId" type="java.lang.Integer">
<column name="site_id" />
</property>
<property name="templateName" type="string">
<column name="template_name" not-null="true" />
</property>
<property name="layoutId" type="java.lang.Integer">
<column name="layout_id"/>
</property>
<property name="description" type="string">
<column name="description"/>
</property>
<property name="timeCreated" type="java.sql.Timestamp" not-null="false" insert="true" update="false">
<column name="time_created" not-null="true" />
</property>
<property name="timeUpdated" type="java.sql.Timestamp" not-null="true" insert="true" update="true">
<column name="time_updated" not-null="true" />
</property>
<property name= "templatePageType" type = "java.lang.Integer" >
<column name="template_page_type" not-null="true" />
</property>
<property name= "templateTheme" type = "string" >
<column name="template_theme" />
</property>
</class>
</hibernate-mapping>
而对应的pojo是:
public class OfficeTemplate
private Integer templateId;
private String officeTemplateId;
private int siteId;
private String templateName;
private int layoutId;
private String description;
private Timestamp timeCreated;
private Timestamp timeUpdated;
private int templatePageType;
private String templateTheme;
我的 saveOrUpdate() 函数如下:
getCurrentSession().saveOrUpdate(siteTemplate);
【问题讨论】:
【参考方案1】:使用 double 作为主键可能是个坏主意。来自 postgreSQL 文档:
数据类型实数和双精度是不精确的, 可变精度数值类型。[...] 不精确意味着某些值不能精确地转换为 内部格式并存储为近似值,因此 存储和 检索值可能会显示出细微的差异。
一般来说,使用浮点表示的类型作为键听起来真的很危险。如果您没有充分的理由这样做,您应该将该类型更改回int
。如果你需要比这更大的东西,你可以选择bigint
。
【讨论】:
@Gregely Basco,我需要这个值是双倍的,不是在这里,而是在我引用该字段值的许多地方。但这是导致此异常的问题吗 需要这个值是可以的,只是不要将它用作键。改为使用序列来生成正确的密钥。是的,很可能这就是你的问题。浮点数永远不是精确的。当您认为它是“1.0”时,它可以是“1.0000000001”。因此,当 Hibernate 尝试通过搜索“1.0”来更新您的记录时,它什么也没找到,然后您会收到错误消息(更新的意外行数:0)。 那么您需要使用更新的架构、代码等来更新您的问题。 对。我看到你现在正在使用一个序列。但是您仍在使用“双”类型?另外,您在没有堆栈跟踪的情况下发布了该异常。 我通过创建一个新的整数字段并将其用作主键来解决此问题以上是关于在休眠 saveOrUpdate() 期间获取 org.hibernate.StaleStateException的主要内容,如果未能解决你的问题,请参考以下文章