MyBatis BatchExecutor insert exception

461 views
Skip to first unread message

Yevgeny Krasik

unread,
Feb 29, 2012, 9:41:07 AM2/29/12
to mybatis-user
Hi,

I'm using MyBatis-3.0.6 ,MyBatis-Spring-1.0.2, Spring-3.0.6, MySQL-5.5
and MySQL-connector 5.1.8.

I'm trying to insert a few records using BatchExecutor, but I'm
getting the following exception:
org.apache.ibatis.exceptions.PersistenceException:
### Error committing transaction. Cause:
org.apache.ibatis.executor.ExecutorException: Error getting generated
key or setting result to parameter object. Cause:
java.lang.NullPointerException
### Cause: org.apache.ibatis.executor.ExecutorException: Error getting
generated key or setting result to parameter object. Cause:
java.lang.NullPointerException
at
org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:
8)
at
org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:
147)
at
org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:
139)
at com.user.dao.TempTest.testUsersBatch(TempTest.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod
$1.runReflectiveCall(FrameworkMethod.java:45)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:
42)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:
20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:
28)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:
30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
68)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:
28)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:
30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)
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.processAfter(Jdbc3KeyGenerator.java:
65)
at
org.apache.ibatis.executor.BatchExecutor.doFlushStatements(BatchExecutor.java:
86)
at
org.apache.ibatis.executor.BaseExecutor.flushStatements(BaseExecutor.java:
97)
at
org.apache.ibatis.executor.BaseExecutor.flushStatements(BaseExecutor.java:
92)
at org.apache.ibatis.executor.BaseExecutor.commit(BaseExecutor.java:
177)
at
org.apache.ibatis.executor.CachingExecutor.commit(CachingExecutor.java:
80)
at
org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:
144)
... 29 more
Caused by: java.lang.NullPointerException
at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
at java.util.TreeMap.getEntryUsingComparator(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.get(Unknown Source)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1117)
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:3002)
at
org.apache.ibatis.type.LongTypeHandler.getNullableResult(LongTypeHandler.java:
17)
at
org.apache.ibatis.type.LongTypeHandler.getNullableResult(LongTypeHandler.java:
8)
at
org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:
29)
at
org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processAfter(Jdbc3KeyGenerator.java:
50)
... 35 more

Here is a sample code:

public void testUsersBatch() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("testing-
context.xml");
SqlSessionFactory factory = ctx.getBean("sqlSessionFactory");
SqlSession sqlSession = factory.openSession(ExecutorType.BATCH);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

final int numUsers = 10;
for (int i = 0; i < numUsers; i++) {
userMapper.insert(new UserImpl("" + i));
}

sqlSession.commit();
}

public class UserImpl implements User {
private String name;

public UserImpl(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

public interface UserMapper {
public void insert(User user);
}

UserMapper.xml:
<insert id="insert" parameterType="UserImpl" useGeneratedKeys="true"
keyProperty="id">
INSERT into Users (Name) VALUES (#{name})
</insert>

I know that in Spring I'm supposed to create a SqlSessionTemplate bean
and this is not what I'm doing, but this was supposed to simply be
proof-of-concept. I'm not using a DAO for this reason as well.

Also, I tried using MyBatis-3.1.0-SNAPSHOT and MyBatis-Spring-1.1.0-
SNAPSHOT, and it works on these snapshots.

So, is there any way to make this work until the snapshots are
released?

Thank you,
Yevgeny.

Eduardo Macarron

unread,
Feb 29, 2012, 9:54:20 AM2/29/12
to mybati...@googlegroups.com
Yevgeny, could you try 3.1 to see if the problem is fixed?

2012/2/29 Yevgeny Krasik <ykr...@gmail.com>:

Yevgeny Krasik

unread,
Feb 29, 2012, 10:06:10 AM2/29/12
to mybatis-user
Hi Eduardo,

Thanks for the reply. I've already tried with 3.1-SNAPSHOT and it
works, however I prefer not to use a snapshot for production code.
Is there anything else I can do until the snapshot is released?

Best Regards,
Yevgeny.

On Feb 29, 4:54 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:
> Yevgeny, could you try 3.1 to see if the problem is fixed?
>
> 2012/2/29 Yevgeny Krasik <ykra...@gmail.com>:

Eduardo

unread,
Feb 29, 2012, 10:41:58 AM2/29/12
to mybatis-user
Sorry Yevgeny, but I am afraid there is no workaround.
Reply all
Reply to author
Forward
0 new messages