View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.submitted.no_param_type;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.Reader;
21  import java.util.List;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.executor.BatchResult;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.ExecutorType;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class NoParamTypeTest {
34  
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create a SqlSessionFactory
40      try (
41          Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/no_param_type/mybatis-config.xml")) {
42        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43      }
44  
45      // populate in-memory database
46      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47          "org/apache/ibatis/submitted/no_param_type/CreateDB.sql");
48    }
49  
50    @Test
51    void shouldAcceptDifferentTypeInTheSameBatch() {
52      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
53        ObjA a = new ObjA();
54        a.setId(1);
55        a.setName(111);
56        sqlSession.insert("insertUser", a);
57        ObjB b = new ObjB();
58        b.setId(2);
59        b.setName("222");
60        sqlSession.insert("insertUser", b);
61        List<BatchResult> batchResults = sqlSession.flushStatements();
62        batchResults.clear();
63        sqlSession.clearCache();
64        sqlSession.commit();
65        List<User> users = sqlSession.selectList("selectUser");
66        assertEquals(2, users.size());
67      }
68    }
69  
70    public static class ObjA {
71      private Integer id;
72  
73      private Integer name;
74  
75      public Integer getId() {
76        return id;
77      }
78  
79      public void setId(Integer id) {
80        this.id = id;
81      }
82  
83      public Integer getName() {
84        return name;
85      }
86  
87      public void setName(Integer name) {
88        this.name = name;
89      }
90    }
91  
92    public static class ObjB {
93      private Integer id;
94  
95      private String name;
96  
97      public Integer getId() {
98        return id;
99      }
100 
101     public void setId(Integer id) {
102       this.id = id;
103     }
104 
105     public String getName() {
106       return name;
107     }
108 
109     public void setName(String name) {
110       this.name = name;
111     }
112   }
113 }