我尽量叙事的清楚点吧,可能稍微啰嗦点。报错内容让我很头疼,我都开始怀疑mybatits 3.0.2 和mysql 5.1是不是本身就有bug了。
请大神们稍事略过下面的代码,然后我再叙述
项目使用到了spring 3 + mybatits 3.0.2 + mysql 5.1
在存储历史记录是报错。偶尔报错,感觉应该是万分之几的概率吧。
Caused by: org.springframework.dao.DuplicateKeyException: SqlSession operation; SQL []; Duplicate entry '5879889-35399' for key 'user_id'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5879889-35399' for key 'user_id' at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:241) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) at org.mybatis.spring.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:364) at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:344) at org.mybatis.spring.SqlSessionTemplate.execute(SqlSessionTemplate.java:173) at org.mybatis.spring.SqlSessionTemplate.execute(SqlSessionTemplate.java:155) at org.mybatis.spring.SqlSessionTemplate$7.invoke(SqlSessionTemplate.java:339) at $Proxy59.addFavorite(Unknown Source) at -------------------------------------.addFavorite(---------.java:104) at -------------------------------------$$FastClassByCGLIB$$bf29fae7.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625) at ------------------------------------$$EnhancerByCGLIB$$1aaa1136.addFavorite(<generated>) at-------------------------------------.addFavorite(--------.java:277) ... 32 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '58179889-353994' for key 'user_id' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:989) at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:22) at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:51) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:29) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:73) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:43) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:102) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:91) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35) at $Proxy59.addFavorite(Unknown Source) at sun.reflect.GeneratedMethodAccessor926.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:342) ... 48 more
上面一堆横杆是我替换了我项目的路径或者类名,
异常中得Duplicate entry '5879889-35399' for key 'user_id'我解释一下
5859889是用户id,35399是书籍id,我再写sql的时候,明明是写对对应的字段的。但是在写入数据库的时候就报错了。
sql如下
<insert id="addFavorite" useGeneratedKeys="true" keyProperty="id" > INSERT INTO favorites (<include refid="favorite.columns" /> <![CDATA[ ) VALUES ( #{id} , #{userId} , #{bookId} , NOW() ) ]]> </insert>
业务代码如下
favorite = new Favorite(); favorite.setBookId(bookInfo.getId()); favorite.setUserId(user.getId()); FavoriteFacade.addFavorite(favorite);// 加入收藏
对于favorite的bookId和userId我都设置的是Integer类型。
不知道为什么,两个字段的内容,再写入数据库的时候,就写在了一块。中间的 - ,是咋回事啊,哪里冒出来的啊????数据库中user_id是int类型。
项目跑了许久了。但是时不时就报着个错误。我感觉我的代码,写的没问题。所以就开始怀疑mybatits 3.0.2 和mysql 5.1是不是本身就有bug了。
大神们,给点建议吧~~~