oraclet往购买成功表中插入数据时,无法获取主键

来源:4-9 DAO层单元测试编码和问题排查(下)

慕粉3727206

2016-11-11 14:34

1、mapper是这样的。在网上没有查到insert ignore into对应的oracle写法,暂时这样写了。

<insert id="insertSuccessKilled">

insert into success_killed(seckill_id,user_phone)

values (#{seckillId}, #{userPhone})

</insert>

2、报错信息是这样的,Error getting generated key or setting result to parameter object. Cause: java.lang.NullPointerException

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.lang.NullPointerException

at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79)

at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)

at com.sun.proxy.$Proxy16.insert(Unknown Source)

at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:279)

at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57)

at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)

at com.sun.proxy.$Proxy17.insertSuccessKilled(Unknown Source)

at org.dao.SuccessKilledDaoTest.testInsertSuccessKilled(SuccessKilledDaoTest.java:20)

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

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

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

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

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.lang.NullPointerException

at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processBatch(Jdbc3KeyGenerator.java:71)

at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processAfter(Jdbc3KeyGenerator.java:45)

at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:50)

at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)

at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)

at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)

at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)

at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)

at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)

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

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

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

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

at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)

... 35 more

Caused by: java.lang.NullPointerException

at oracle.jdbc.driver.AutoKeyInfo.initMetaDataKeyFlag(AutoKeyInfo.java:404)

at oracle.jdbc.driver.AutoKeyInfo.initMetaData(AutoKeyInfo.java:392)

at oracle.jdbc.driver.OracleReturnResultSet.getMetaData(OracleReturnResultSet.java:77)

at com.mchange.v2.c3p0.impl.NewProxyResultSet.getMetaData(NewProxyResultSet.java:47)

at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processBatch(Jdbc3KeyGenerator.java:55)

... 48 more

以上可以往数据库中插件数据,但JUnit单元测试时会报上述错误,请问是怎么回事,该怎么解决?

写回答 关注

2回答

  • 慕粉3727206
    2016-11-28 22:55:10

    <insert id="insertSuccessKilled" useGeneratedKeys="false">

      insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(success_killed(seckill_id,user_phone)) */ into 

      success_killed (seckill_id,user_phone)

      values(#{seckillId},#{userPhone})

      </insert>

    此问题可以这样解决,useGeneratedKeys="false"不使用mybatis自动生成的主键,而使用建表时的组合主键;insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(success_killed(seckill_id,user_phone)) */ into 可以忽略oracle的“违反惟一性原则”出现的错误提示。

  • oracleocp
    2016-11-20 02:16:06

    oracle不支持自增类型的字段, 需要先创建序列, 直接在SQL语句中写序列名.nextval

    慕粉3727...

    这个表是组合主键啊

    2016-11-28 22:46:56

    共 1 条回复 >

Java高并发秒杀API之业务分析与DAO层

Java实现高并发秒杀API的第一门课,还等什么,赶快来加入吧

87426 学习 · 496 问题

查看课程

相似问题