Skip to content

Commit 75d2cbe

Browse files
committed
Fix for issue #5
1 parent 3322ea9 commit 75d2cbe

File tree

8 files changed

+183
-8
lines changed

8 files changed

+183
-8
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBoun
146146
return list;
147147
}
148148

149-
public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key) {
149+
public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType) {
150150
if (closed) throw new ExecutorException("Executor was closed.");
151-
DeferredLoad deferredLoad = new DeferredLoad(ms, resultObject, property, key, localCache, configuration);
151+
DeferredLoad deferredLoad = new DeferredLoad(ms, resultObject, property, key, localCache, configuration, targetType);
152152
if (deferredLoad.canLoad()) {
153153
deferredLoad.load();
154154
} else {
155-
deferredLoads.add(new DeferredLoad(ms, resultObject, property, key, localCache, configuration));
155+
deferredLoads.add(new DeferredLoad(ms, resultObject, property, key, localCache, configuration, targetType));
156156
}
157157
}
158158

@@ -280,6 +280,7 @@ private static class DeferredLoad {
280280

281281
private final MetaObject resultObject;
282282
private final String property;
283+
private final Class<?> targetType;
283284
private final CacheKey key;
284285
private final PerpetualCache localCache;
285286
private final ObjectFactory objectFactory;
@@ -290,13 +291,15 @@ public DeferredLoad(MappedStatement mappedStatement,
290291
String property,
291292
CacheKey key,
292293
PerpetualCache localCache,
293-
Configuration configuration) {
294+
Configuration configuration,
295+
Class<?> targetType) { // issue #781
294296
this.resultObject = resultObject;
295297
this.property = property;
296298
this.key = key;
297299
this.localCache = localCache;
298300
this.objectFactory = configuration.getObjectFactory();
299301
this.resultExtractor = new ResultExtractor(configuration, objectFactory);
302+
this.targetType = targetType;
300303
}
301304

302305
public boolean canLoad() {
@@ -306,7 +309,6 @@ public boolean canLoad() {
306309
public void load() {
307310
@SuppressWarnings( "unchecked" ) // we suppose we get back a List
308311
List<Object> list = (List<Object>) localCache.getObject(key);
309-
Class<?> targetType = resultObject.getSetterType(property);
310312
Object value = resultExtractor.extractObjectFromList(list, targetType);
311313
resultObject.setValue(property, value);
312314
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public boolean isCached(MappedStatement ms, CacheKey key) {
144144
throw new UnsupportedOperationException("The CachingExecutor should not be used by result loaders and thus isCached() should never be called.");
145145
}
146146

147-
public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key) {
147+
public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType) {
148148
throw new UnsupportedOperationException("The CachingExecutor should not be used by result loaders and thus deferLoad() should never be called.");
149149
}
150150

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public interface Executor {
4848

4949
void clearLocalCache();
5050

51-
void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key);
51+
void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);
5252

5353
Transaction getTransaction();
5454

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ protected Object getNestedQueryMappingValue(ResultSet rs, MetaObject metaResultO
476476
if (nestedQueryCacheObject != null && nestedQueryCacheObject instanceof List) {
477477
value = resultExtractor.extractObjectFromList((List<Object>)nestedQueryCacheObject, targetType);
478478
} else if (executor.isCached(nestedQuery, key)) {
479-
executor.deferLoad(nestedQuery, metaResultObject, property, key);
479+
executor.deferLoad(nestedQuery, metaResultObject, property, key, targetType);
480480
} else {
481481
final ResultLoader resultLoader = new ResultLoader(configuration, executor, nestedQuery, nestedQueryParameterObject, targetType, key, nestedBoundSql);
482482
if (configuration.isLazyLoadingEnabled()) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2013 The MyBatis Team
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.associationtype;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.junit.Assert;
24+
25+
import org.apache.ibatis.io.Resources;
26+
import org.apache.ibatis.jdbc.ScriptRunner;
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.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class AssociationTypeTest {
34+
35+
private static SqlSessionFactory sqlSessionFactory;
36+
37+
@BeforeClass
38+
public static void setUp() throws Exception {
39+
// create a SqlSessionFactory
40+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/associationtype/mybatis-config.xml");
41+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42+
reader.close();
43+
44+
// populate in-memory database
45+
SqlSession session = sqlSessionFactory.openSession();
46+
Connection conn = session.getConnection();
47+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/associationtype/CreateDB.sql");
48+
ScriptRunner runner = new ScriptRunner(conn);
49+
runner.setLogWriter(null);
50+
runner.runScript(reader);
51+
reader.close();
52+
session.close();
53+
}
54+
55+
@Test
56+
public void shouldGetAUser() {
57+
SqlSession sqlSession = sqlSessionFactory.openSession();
58+
try {
59+
List<Map> results = sqlSession.selectList("getUser");
60+
for (Map r : results) {
61+
Assert.assertEquals(String.class, r.get("a1").getClass());
62+
Assert.assertEquals(String.class, r.get("a2").getClass());
63+
}
64+
} finally {
65+
sqlSession.close();
66+
}
67+
}
68+
69+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--
2+
-- Copyright 2009-2012 The MyBatis Team
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+
24+
insert into users (id, name) values(1, 'User1');
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-2012 The MyBatis Team
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.associationtype.Mapper">
22+
23+
<resultMap id="sampleHashResult" type="hashmap">
24+
<result property="f1" column="f1" />
25+
<result property="f2" column="f2" />
26+
<association property="a1" javaType="java.lang.String"
27+
column="{param1=f1}" select="associationTest" />
28+
<association property="a2" javaType="java.lang.String"
29+
column="{param1=f1}" select="associationTest" />
30+
</resultMap>
31+
32+
<select id="getUser" resultMap="sampleHashResult">
33+
SELECT id as f1, name as f2 from users
34+
</select>
35+
36+
<select id="associationTest" resultType="java.lang.String">
37+
select id from users
38+
</select>
39+
40+
</mapper>
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-2012 The MyBatis Team
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:associationtype" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper resource="org/apache/ibatis/submitted/associationtype/Mapper.xml" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)