MySQL查询报Duplicate entry '' for key 'PRIMARY错, 没错, 查询报的
日志是一个商机信息跟着一个同步工程信息, 异常中的那个1588162468514
不知道是哪里来的, 商机信息和同步工程信息的表里都没有这个值. 报错的地方只有商机信息, 同步工程信息没有打印出来, 这俩日志中间是有一个查询操作的:
下面是repository中的方法:
然后是异常信息和代码中的位置:
完整的异常:
SQL Error: 1062, SQLState: 23000
2020/11/2 下午4:25:412020-11-02 16:25:41.611 ERROR 1 --- [nio-8221-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Duplicate entry '1588162468514' for key 'PRIMARY'
2020/11/2 下午4:25:412020-11-02 16:25:41.629 ERROR 1 --- [nio-8221-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
2020/11/2 下午4:25:41
2020/11/2 下午4:25:41com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1588162468514' for key 'PRIMARY'
2020/11/2 下午4:25:41 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
2020/11/2 下午4:25:41 at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.2.0.jar!/:na]
2020/11/2 下午4:25:41 at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar!/:na]
2020/11/2 下午4:25:41 at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3171) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3686) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1440) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.query.internal.NativeQueryImpl.beforeQuery(NativeQueryImpl.java:261) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1503) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1553) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
2020/11/2 下午4:25:41 at com.faujor.project.service.impl.ProjAssociationServiceImpl.asyncBusiOppoForAsso(ProjAssociationServiceImpl.java:209) ~[classes!/:0.0.1]
2020/11/2 下午4:25:41 at com.faujor.project.service.impl.ProjAssociationServiceImpl$$FastClassBySpringCGLIB$$d32c2c23.invoke(<generated>) ~[classes!/:0.0.1]
2020/11/2 下午4:25:41 at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at com.faujor.project.service.impl.ProjAssociationServiceImpl$$EnhancerBySpringCGLIB$$3d4a5acc.asyncBusiOppoForAsso(<generated>) ~[classes!/:0.0.1]
2020/11/2 下午4:25:41 at com.faujor.project.controller.ProjAssociationController.asyncBusiOppoForAsso(ProjAssociationController.java:666) ~[classes!/:0.0.1]
2020/11/2 下午4:25:41 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
2020/11/2 下午4:25:41 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90) ~[spring-boot-actuator-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117) ~[spring-boot-actuator-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106) ~[spring-boot-actuator-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_202]
2020/11/2 下午4:25:41 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_202]
2020/11/2 下午4:25:41 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.13.jar!/:9.0.13]
2020/11/2 下午4:25:41 at java.lang.Thread.run(Thread.java:748) [na:1.8.0_202]
查询为什么会报这么个错误呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于给的信息不多,不过依照
hibernate
的事务处理性质,我给予以下几个点来梳理哈问题,题主可以根据这些点去check
一下* 首先这个报错
Duplicate entry '1588162468514' for key 'PRIMARY'
是数据库报出来的你去给数据库执行
sql
插入操作,但是已有主键存在,所以sql
报错* 那为啥明明是查询操作,却执行了插入操作呢?
从堆栈上也可以看到,开始确实是
list()
操作,但是关键就是出在了这个list()
操作之前,需要进行一次flush
*
hibernate
的flush
会把当前Session
开启的事务中所有登记的操作全部发给数据库执行一遍(但不提交事务,这是与commit
的区别),并且按照insert,update...
等顺序去执行所以此时我猜想,你的方法
ProjAssociationServiceImpl.asyncBusiOppoForAsso
应该是有个事务@Transactional
的(就算没有,asyncBusiOppoForAsso
再外层的方法估计有,总之肯定被包到一个事务里了)* 因为有了事务,恰好你的查询方法
ProjAssociation findByBusiCode(String busiCode)
使用了NativeQueryImpl
,而NativeQueryImpl
执行beforeQuery
时要先判断一下是否需要flush
,也就是方法NativeQueryImpl.shouldFlush
* 因此执行
flush
的话,ProjAssociationServiceImpl.asyncBusiOppoForAsso
中所有在findByBusiCode
之前登记的insert
都要执行了。那这里面有2条以上sql主键重复问题,当然就会报错,虽然不是你查询的问题,但是恰好这个查询相当于提前执行了一次flush
结合以上的点,那要检查的东西就很简单了,去找找在
asyncBusiOppoForAsso
内(或外层事务包裹范围内),findByBusiCode
之前做过的save()
操作,并且着重检查save()
时的实体的主键生成策略。因为没有其他信息,我下面只是举个例子
虽然最后只是执行一个查询,但是前面有两个
User
的save
操作,如果User
的主键生成策略导致最终user1
,user2
主键都是相同的,那repository.findByName
执行时会触发一次flush
,那此时之前登记的user1
,user2
的insert
操作就会一起执行一遍,导致最终报错以上就是我的看法吧,不过没有复现你的问题,只能说提供一些参考意见,希望有所帮助~那。。。拜了个拜~(* ̄rǒ ̄)
谢邀。
报错是这一行
ProjAssociationServiceImpl.asyncBusiOppoForAsso(ProjAssociationServiceImpl.java:209)