Java Code Examples for org.apache.ibatis.session.SqlSession#flushStatements()

The following examples show how to use org.apache.ibatis.session.SqlSession#flushStatements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NoParamTypeTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 2
Source File: InsertTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Ignore // Not supported yet in PostgreSQL
@Test
public void testInsertMappedBatch() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
    Name name = new Name();
    name.setFirstName("Fred");
    name.setLastName("Flintstone");
    mapper.insertNameMapped(name);
    Name name2 = new Name();
    name2.setFirstName("Wilma");
    name2.setLastName("Flintstone");
    mapper.insertNameMapped(name2);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    assertNotNull(name.getId());
    assertNotNull(name2.getId());
    assertEquals(1, batchResults.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 3
Source File: BatchKeysTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insert", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insert", user2);
    sqlSession.flushStatements();
    assertEquals(new Integer(50), user1.getId());
    assertEquals(new Integer(50), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("select");
  Assert.assertTrue(users.size() == 2);
}
 
Example 4
Source File: BatchKeysTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJdbc3() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insertIdentity", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insertIdentity", user2);
    sqlSession.flushStatements();
    assertEquals(Integer.valueOf(0), user1.getId());
    assertEquals(Integer.valueOf(1), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("selectIdentity");
  Assert.assertTrue(users.size() == 2);
}
 
Example 5
Source File: NoParamTypeTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 6
Source File: InsertTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Ignore // Not supported yet in PostgreSQL
@Test
public void testInsertMappedBatch() throws Exception {
  Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml");
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
    Name name = new Name();
    name.setFirstName("Fred");
    name.setLastName("Flintstone");
    mapper.insertNameMapped(name);
    Name name2 = new Name();
    name2.setFirstName("Wilma");
    name2.setLastName("Flintstone");
    mapper.insertNameMapped(name2);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    assertNotNull(name.getId());
    assertNotNull(name2.getId());
    assertEquals(1, batchResults.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 7
Source File: BatchKeysTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insert", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insert", user2);
    sqlSession.flushStatements();
    assertEquals(new Integer(50), user1.getId());
    assertEquals(new Integer(50), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("select");
  Assert.assertTrue(users.size() == 2);
}
 
Example 8
Source File: BatchKeysTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJdbc3() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insertIdentity", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insertIdentity", user2);
    sqlSession.flushStatements();
    assertEquals(Integer.valueOf(0), user1.getId());
    assertEquals(Integer.valueOf(1), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("selectIdentity");
  Assert.assertTrue(users.size() == 2);
}