User BATCH mode to execute batch insert in h2 and the generated key are the same

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

User BATCH mode to execute batch insert in h2 and the generated key are the same

rocky
I execute batch insert like this:

List<Animal> animalList = new ArrayList<>();

SqlSession sqlSession = MyBatisHelper.getSqlSessionFactory().openSession(ExecutorType.BATCH);
AnimalMapper animalMapper = sqlSession.getMapper(AnimalMapper.class);

Animal animal = null;
for (int i=0; i<10; i++) {
    animal
= new Animal();
    animal
.setName("dog" + i);
    animal
.setWeight(i);
    animalList
.add(animal);

    animalMapper
.insert(animal);
}

sqlSession
.commit();
sqlSession
.close();

I use the SelectKey to return the generated id. The statement is "call identity()".

After run,  the id of all them is 10. 

Why? MyBatis don't support or maybe I have some wrong?


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User BATCH mode to execute batch insert in h2 and the generated key are the same

rocky
I know, it must use the Jdbc3KeyGenerator.

On Sunday, January 28, 2018 at 10:58:39 PM UTC+8, rocky_hu wrote:
I execute batch insert like this:

List<Animal> animalList = new ArrayList<>();

SqlSession sqlSession = MyBatisHelper.getSqlSessionFactory().openSession(ExecutorType.BATCH);
AnimalMapper animalMapper = sqlSession.getMapper(AnimalMapper.class);

Animal animal = null;
for (int i=0; i<10; i++) {
    animal
= new Animal();
    animal
.setName("dog" + i);
    animal
.setWeight(i);
    animalList
.add(animal);

    animalMapper
.insert(animal);
}

sqlSession
.commit();
sqlSession
.close();

I use the SelectKey to return the generated id. The statement is "call identity()".

After run,  the id of all them is 10. 

Why? MyBatis don't support or maybe I have some wrong?


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.