主键是自增1,在保存数据的时候提示主键冲突:请问怎么解决?

2018-01-18 12:06:51

[ERROR]-[Thread: qtp1002276202-29]-[com.jfinal.core.ActionHandler.handle()]: /ajax/subjectSelect

com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '8' for key 'PRIMARY'

at com.jfinal.plugin.activerecord.Model.save(Model.java:452)

at com.javen.model.SubjectSelect.saveSubjectSelect(SubjectSelect.java:52)

at com.javen.controller.AjaxController.subjectSelect(AjaxController.java:79)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at com.jfinal.aop.Invocation.invoke(Invocation.java:73)

at com.jfinal.validate.Validator.intercept(Validator.java:86)

at com.jfinal.aop.Invocation.invoke(Invocation.java:67)

at com.jfinal.core.ActionHandler.handle(ActionHandler.java:74)

at com.jfinal.plugin.druid.DruidStatViewHandler.handle(DruidStatViewHandler.java:75)

at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:73)

at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)

at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)

at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)

at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)

at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)

at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)

at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)

at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)

at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)

at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)

at org.eclipse.jetty.server.Server.handle(Server.java:365)

at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)

at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:937)

at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:998)

at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)

at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)

at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)

at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)

at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)

at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)

at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)

at java.lang.Thread.run(Thread.java:745)

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '8' for key 'PRIMARY'

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)

at com.mysql.jdbc.Util.getInstance(Util.java:387)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:934)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2550)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)

at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)

at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)

at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)

at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2723)

at com.alibaba.druid.wall.WallFilter.preparedStatement_executeUpdate(WallFilter.java:633)

at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2721)

at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1069)

at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeUpdate(FilterEventAdapter.java:491)

at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2721)

at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeUpdate(PreparedStatementProxyImpl.java:158)

at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:253)

at com.jfinal.plugin.activerecord.Model.save(Model.java:447)

... 35 more


评论区

lyh061619

2018-01-18 12:20

你没有创建主键索引的吧?没有的话键个,主键索引确保主键是唯一的,也就是不可重复的。

lyh061619

2018-01-18 12:22

也就是设置为:primary key

lyh061619

2018-01-18 12:28

另外检查下你保存时是否引起线程安全问题,共享对象,如果是这样的话你保存时就会有两相同主键的记录被同时保存了,然后就会提示你上面反馈的主键重复了,所以解决办法前提是:必须主键创建并配置好后设置为自增,保存数据如Test test = new test(); 一定要记录new给对象重新分配堆内存,避免共享对象创建重复数据。

lyh061619

2018-01-18 12:32

最后,你反馈问题,请把问题移至反馈模块感谢配合下。

JFinal

2018-01-18 12:35

据猜测你是使用了 static 声明的 dao.save()

static 声明的对象是全局共享的,当你第一次 dao.save() 时 id 会生成到这个 dao 对象中,当第二次 dao.save() 时,id 已存在,并且是原来的值,必然出错

dao 这个对象在创建的时候要调用一次其 dao() 方法,如下所示:
static User dao = new User().dao();
这样就可以 避免你 dao.save() 的错误

wjl

2018-02-10 08:45

@JFinal static 共享的话可以设置id为null?

auther2119

2018-03-03 10:46

检查一下表的id有没有从0开始一次递增,再说怎么解决

auther2119

2018-03-04 08:32

@智诚博辉 写一个static 方法,产生id,规则仍可以是自增1,调用model.save之前先设置其id再保存,就可以避免重复。

auther2119

2018-03-14 15:48

@智诚博辉 检查一下表的设计,有没有勾上自动加1

auther2119

2018-03-14 15:49

@智诚博辉 之前说的static方法有多线程问题。

热门分享

扫码入社