Skip to content

Commit ff8db5c

Browse files
committed
#421 Merged into 3.3.x branch
1 parent 9589554 commit ff8db5c

File tree

7 files changed

+269
-1
lines changed

7 files changed

+269
-1
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap,
394394
&& (value != null || (configuration.isCallSettersOnNulls() && !metaObject.getSetterType(property).isPrimitive()))) {
395395
metaObject.setValue(property, value);
396396
}
397-
if (value != null || value == DEFERED) {
397+
if (property != null && (value != null || value == DEFERED)) {
398398
foundValues = true;
399399
}
400400
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2009-2015 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+
* http://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.emptycollection;
17+
18+
import java.util.List;
19+
20+
interface Dao {
21+
List<TodoLists> selectWithEmptyList();
22+
List<TodoLists> selectWithNonEmptyList();
23+
List<TodoLists> selectWithNonEmptyList_noCollectionId();
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
4+
<mapper namespace="org.apache.ibatis.submitted.emptycollection.Dao">
5+
<select id="selectWithEmptyList" resultMap="selectList">
6+
select 1 id, null "order", null description from (VALUES ('dummy')) dummy(dummy)
7+
</select>
8+
9+
<select id="selectWithNonEmptyList" resultMap="selectList">
10+
select 1 id, 1 "order", 'a description' description from (VALUES ('dummy')) dummy(dummy)
11+
union all select 1 id, 2 "order", 'a 2nd description' description from (VALUES ('dummy')) dummy(dummy)
12+
union all select 2 id, 1 "order", 'a description' description from (VALUES ('dummy')) dummy(dummy)
13+
</select>
14+
15+
<select id="selectWithNonEmptyList_noCollectionId" resultMap="selectList_noCollectionId">
16+
select 1 id, 1 "order", 'a description' description from (VALUES ('dummy')) dummy(dummy)
17+
union all select 1 id, 2 "order", 'a 2nd description' description from (VALUES ('dummy')) dummy(dummy)
18+
union all select 2 id, 1 "order", 'a description' description from (VALUES ('dummy')) dummy(dummy)
19+
</select>
20+
21+
22+
<resultMap id="selectList" type="org.apache.ibatis.submitted.emptycollection.TodoLists">
23+
<id column="id" property="id" />
24+
<collection property="todoItems" ofType="org.apache.ibatis.submitted.emptycollection.TodoItem" javaType="list">
25+
<id column="id" />
26+
<id column="order" property="order" />
27+
<result column="description" property="description"/>
28+
</collection>
29+
</resultMap>
30+
31+
<resultMap id="selectList_noCollectionId" type="org.apache.ibatis.submitted.emptycollection.TodoLists">
32+
<id column="id" property="id" />
33+
<collection property="todoItems" ofType="org.apache.ibatis.submitted.emptycollection.TodoItem" javaType="list">
34+
<id column="order" property="order" />
35+
<result column="description" property="description"/>
36+
</collection>
37+
</resultMap>
38+
39+
</mapper>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2009-2015 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+
* http://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.emptycollection;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
import java.util.List;
21+
22+
import org.apache.ibatis.io.Resources;
23+
import org.apache.ibatis.jdbc.ScriptRunner;
24+
import org.apache.ibatis.session.SqlSession;
25+
import org.apache.ibatis.session.SqlSessionFactory;
26+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.junit.After;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
public class DaoTest {
33+
private Dao dao;
34+
private SqlSession sqlSession;
35+
36+
@Before
37+
public void setUp() throws Exception {
38+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/emptycollection/mybatis-config.xml");
39+
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
reader.close();
41+
42+
sqlSession = sqlSessionFactory.openSession();
43+
Connection conn = sqlSession.getConnection();
44+
ScriptRunner runner = new ScriptRunner(conn);
45+
runner.setLogWriter(null);
46+
dao = sqlSession.getMapper(Dao.class);
47+
}
48+
49+
@After
50+
public void tearDown() throws Exception {
51+
sqlSession.close();
52+
}
53+
54+
@Test
55+
public void testWithEmptyList() throws Exception {
56+
final List<TodoLists> actual = dao.selectWithEmptyList();
57+
Assert.assertEquals(1, actual.size());
58+
final List<TodoItem> todoItems = actual.get(0).getTodoItems();
59+
Assert.assertEquals("expect " + todoItems + " to be empty", 0, todoItems.size());
60+
}
61+
62+
@Test
63+
public void testWithNonEmptyList() throws Exception {
64+
final List<TodoLists> actual = dao.selectWithNonEmptyList();
65+
checkNonEmptyList(actual);
66+
}
67+
68+
@Test
69+
public void testWithNonEmptyList_noCollectionId() throws Exception {
70+
final List<TodoLists> actual = dao.selectWithNonEmptyList_noCollectionId();
71+
72+
checkNonEmptyList(actual);
73+
}
74+
75+
private void checkNonEmptyList(final List<TodoLists> actual) {
76+
// Assert.assertEquals("[List(1)=[a description(1), a 2nd description(2)], List(2)=[a description(1)]]", actual.toString());
77+
Assert.assertEquals(2, actual.size());
78+
79+
Assert.assertEquals(2, actual.get(0).getTodoItems().size());
80+
Assert.assertEquals(1, actual.get(0).getTodoItems().get(0).getOrder());
81+
Assert.assertEquals("a description", actual.get(0).getTodoItems().get(0).getDescription().trim());
82+
Assert.assertEquals(2, actual.get(0).getTodoItems().get(1).getOrder());
83+
Assert.assertEquals("a 2nd description", actual.get(0).getTodoItems().get(1).getDescription().trim());
84+
85+
Assert.assertEquals(1, actual.get(1).getTodoItems().size());
86+
Assert.assertEquals(1, actual.get(1).getTodoItems().get(0).getOrder());
87+
Assert.assertEquals("a description", actual.get(0).getTodoItems().get(0).getDescription().trim());
88+
89+
// We should have gotten three item objects. The first item from the first list and the first item from
90+
// the second list have identical properties, but they should be distinct objects
91+
Assert.assertNotSame(actual.get(0).getTodoItems().get(0), actual.get(1).getTodoItems().get(0));
92+
}
93+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2009-2015 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+
* http://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.emptycollection;
17+
18+
public class TodoItem {
19+
20+
@Override
21+
public String toString() {
22+
return "TodoItem [order=" + order + ", description=" + description + "]";
23+
}
24+
25+
private int order;
26+
private String description;
27+
28+
public int getOrder() {
29+
return order;
30+
}
31+
32+
public void setOrder(int order) {
33+
this.order = order;
34+
}
35+
36+
public String getDescription() {
37+
return description;
38+
}
39+
40+
public void setDescription(String description) {
41+
this.description = description;
42+
}
43+
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2009-2015 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+
* http://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.emptycollection;
17+
18+
import java.util.List;
19+
20+
public class TodoLists {
21+
22+
@Override
23+
public String toString() {
24+
return "TodoLists [id=" + id + ", todoItems=" + todoItems + "]";
25+
}
26+
27+
private int id;
28+
29+
private List<TodoItem> todoItems;
30+
31+
public int getId() {
32+
return id;
33+
}
34+
35+
public void setId(int id) {
36+
this.id = id;
37+
}
38+
39+
public List<TodoItem> getTodoItems() {
40+
return todoItems;
41+
}
42+
43+
public void setTodoItems(List<TodoItem> todoItems) {
44+
this.todoItems = todoItems;
45+
}
46+
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
<configuration>
6+
<environments default="development">
7+
<environment id="development">
8+
<transactionManager type="JDBC">
9+
<property name="" value="" />
10+
</transactionManager>
11+
<dataSource type="UNPOOLED">
12+
<property name="driver" value="org.hsqldb.jdbcDriver" />
13+
<property name="url" value="jdbc:hsqldb:mem:emptycollection" />
14+
<property name="username" value="sa" />
15+
</dataSource>
16+
</environment>
17+
</environments>
18+
<mappers>
19+
<mapper resource="org/apache/ibatis/submitted/emptycollection/Dao.xml" />
20+
</mappers>
21+
</configuration>

0 commit comments

Comments
 (0)