Skip to content

Commit cbacd88

Browse files
committed
Fixes #259 association element fails to map results of the nested select to a primitive array property.
1 parent 62fe78a commit cbacd88

File tree

7 files changed

+255
-2
lines changed

7 files changed

+255
-2
lines changed

src/main/java/org/apache/ibatis/executor/ResultExtractor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ public Object extractObjectFromList(List<Object> list, Class<?> targetType) {
4343
MetaObject metaObject = configuration.newMetaObject(value);
4444
metaObject.addAll(list);
4545
} else if (targetType != null && targetType.isArray()) {
46-
Object[] array = (Object[]) Array.newInstance(targetType.getComponentType(), list.size());
47-
value = list.toArray(array);
46+
Class<?> arrayComponentType = targetType.getComponentType();
47+
Object array = Array.newInstance(arrayComponentType, list.size());
48+
if (arrayComponentType.isPrimitive()) {
49+
for (int i = 0; i < list.size(); i++) {
50+
Array.set(array, i, list.get(i));
51+
}
52+
value = array;
53+
} else {
54+
value = list.toArray((Object[])array);
55+
}
4856
} else {
4957
if (list != null && list.size() > 1) {
5058
throw new ExecutorException("Statement returned more than one row, where no more than one was expected.");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--
2+
-- Copyright 2009-2014 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+
17+
drop table users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20)
22+
);
23+
create table udata (
24+
user_id int,
25+
num int
26+
);
27+
28+
insert into users (id, name) values(1, 'User1');
29+
insert into udata (user_id, num) values(1, 100);
30+
insert into udata (user_id, num) values(1, 300);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2009-2014 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.primitive_array;
17+
18+
public interface Mapper {
19+
20+
User getUser(Integer id);
21+
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2009-2014 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE mapper
18+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
20+
21+
<mapper namespace="org.apache.ibatis.submitted.primitive_array.Mapper">
22+
23+
<resultMap type="org.apache.ibatis.submitted.primitive_array.User" id="userMap">
24+
<id property="id" column="id" />
25+
<result property="name" column="name" />
26+
<association property="num" column="id" select="getNum" />
27+
</resultMap>
28+
29+
<select id="getUser" resultMap="userMap">
30+
select * from users u
31+
where id = #{id}
32+
</select>
33+
34+
<select id="getNum" resultType="_int">
35+
select num from udata
36+
where user_id = #{id}
37+
order by num
38+
</select>
39+
40+
</mapper>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2009-2014 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.primitive_array;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.jdbc.ScriptRunner;
23+
import org.apache.ibatis.session.SqlSession;
24+
import org.apache.ibatis.session.SqlSessionFactory;
25+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26+
import org.junit.Assert;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class PrimitiveArrayTest {
31+
32+
private static SqlSessionFactory sqlSessionFactory;
33+
34+
@BeforeClass
35+
public static void setUp() throws Exception {
36+
// create an SqlSessionFactory
37+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/primitive_array/mybatis-config.xml");
38+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
reader.close();
40+
41+
// populate in-memory database
42+
SqlSession session = sqlSessionFactory.openSession();
43+
Connection conn = session.getConnection();
44+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/primitive_array/CreateDB.sql");
45+
ScriptRunner runner = new ScriptRunner(conn);
46+
runner.setLogWriter(null);
47+
runner.runScript(reader);
48+
reader.close();
49+
session.close();
50+
}
51+
52+
@Test
53+
public void shouldGetAUser() {
54+
SqlSession sqlSession = sqlSessionFactory.openSession();
55+
try {
56+
Mapper mapper = sqlSession.getMapper(Mapper.class);
57+
User user = mapper.getUser(1);
58+
Assert.assertEquals("User1", user.getName());
59+
Assert.assertEquals(2, user.getNum().length);
60+
Assert.assertEquals(100, user.getNum()[0]);
61+
} finally {
62+
sqlSession.close();
63+
}
64+
}
65+
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2009-2014 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.primitive_array;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
private int[] num;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public int[] getNum() {
41+
return num;
42+
}
43+
44+
public void setNum(int[] num) {
45+
this.num = num;
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Copyright 2009-2014 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!DOCTYPE configuration
18+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
19+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
20+
21+
<configuration>
22+
23+
<environments default="development">
24+
<environment id="development">
25+
<transactionManager type="JDBC">
26+
<property name="" value="" />
27+
</transactionManager>
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver" />
30+
<property name="url" value="jdbc:hsqldb:mem:primitivearray" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper class="org.apache.ibatis.submitted.primitive_array.Mapper" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)