请求处理失败;嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法执行 JDBC 批量更新
Posted
技术标签:
【中文标题】请求处理失败;嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法执行 JDBC 批量更新【英文标题】:Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 【发布时间】:2014-10-19 11:11:11 【问题描述】:这是我在 *** 上的第一篇文章!但我是 UIO 的硕士生,所以我不是在得到很多帮助(如果你甚至选择),但也许是一个指向我可以更好地理解问题的指针!如果您不喜欢我展示问题的方式,请对此发表评论,下次会更好!
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
type Exception report
message Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1175)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1251)
org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
no.uio.inf5750.assignment2.dao.hibernate.HibernateStudentDao.getAllStudents(HibernateStudentDao.java:54)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
com.sun.proxy.$Proxy13.getAllStudents(Unknown Source)
no.uio.inf5750.assignment2.service.impl.DefaultStudentSystem.getAllStudents(DefaultStudentSystem.java:168)
no.uio.inf5750.assignment2.gui.controller.ApiController.jsonAddLocation(ApiController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause
org.h2.jdbc.JdbcBatchUpdateException: Unique index or primary key violation: "CONSTRAINT_INDEX_BA ON PUBLIC.STUDENT(LATITUDE)"; SQL statement:
update student set name=?, latitude=?, longitude=? where student_id=? [23001-136]
org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1098)
com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1175)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1251)
org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
no.uio.inf5750.assignment2.dao.hibernate.HibernateStudentDao.getAllStudents(HibernateStudentDao.java:54)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
com.sun.proxy.$Proxy13.getAllStudents(Unknown Source)
no.uio.inf5750.assignment2.service.impl.DefaultStudentSystem.getAllStudents(DefaultStudentSystem.java:168)
no.uio.inf5750.assignment2.gui.controller.ApiController.jsonAddLocation(ApiController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:233)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
来自堆栈跟踪:
no.uio.inf5750.assignment2.dao.hibernate.HibernateStudentDao.getAllStudents(HibernateStudentDao.java:54)
代码:
public Collection<Student> getAllStudents()
Session session = sessionFactory.getCurrentSession();
String hql = "from Student";
Query query = session.createQuery(hql);
return (Collection<Student>)query.list(); <- java:54
【问题讨论】:
【参考方案1】:约束违反异常通常发生在您尝试将数据插入表行时,其值违反了其中的任何设置约束。
请确保插入列的值没有违反约束集 例如,如果列具有唯一索引,则确保该值与其他记录是唯一的。
【讨论】:
【参考方案2】:您的查询正在触发刷新,这会导致您的 ConstraintViolationException。不幸的是,您在保存时没有收到此错误,但您的错误肯定与此查询无关。
在此查询之前调查您的插入。尝试在保存的同时刷新 (.saveAndFlush() / .save(flush:true)) 以尝试立即识别错误的插入,希望 Hibernate 和 JDBC 会给你一个正确的错误消息,告诉你你做错了什么而不是这种不特定的约束违规。
【讨论】:
我的目标是能够改变纬度和经度,但我根本无法在我的限制下。以上是关于请求处理失败;嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法执行 JDBC 批量更新的主要内容,如果未能解决你的问题,请参考以下文章
HTTP状态500-请求处理失败;嵌套的异常是java.lang.NullPointerException?
使用dubbo引用和发布服务时出现的异常:HTTP状态500 - 请求处理失败; 嵌套异常是com.alibaba.dubbo.rpc.RpcException:无法在服务cn.e3mall.serv
Postman:解析多部分 servlet 请求失败;嵌套异常是 java.lang.IllegalStateException, MultipartFile, REST
HTTP 状态 500 - 处理程序处理失败;嵌套异常是 java.lang.UnsatisfiedLinkError: 找不到指定的模块