Skip to content

Commit c7639ad

Browse files
committed
Fixes #9. A column mapped to an unknown object type should produce an
exception.
1 parent 5d0d25b commit c7639ad

File tree

8 files changed

+255
-13
lines changed

8 files changed

+255
-13
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,31 +305,36 @@ protected boolean applyPropertyMappings(ResultSet rs, ResultMap resultMap, List<
305305
if (propertyMapping.isCompositeResult() || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH)))) {
306306
Object value = getPropertyMappingValue(rs, metaObject, propertyMapping, lazyLoader, columnPrefix);
307307
final String property = propertyMapping.getProperty(); // issue #541 make property optional
308-
if (value != OMIT && property != null && (value != null || isCallSettersOnNulls(metaObject.getGetterType(property)))) { // issue #377, call setter on nulls
308+
if (value != OMIT && property != null && (value != null || isCallSettersOnNulls(metaObject.getSetterType(property)))) { // issue #377, call setter on nulls
309309
metaObject.setValue(property, value);
310-
foundValues = true;
310+
foundValues = (value != null) || foundValues;
311311
}
312312
}
313313
}
314314
return foundValues;
315315
}
316-
317-
private boolean isCallSettersOnNulls(Class<?> propertyType){
318-
if(configuration.isCallSettersOnNulls() && !propertyType.isPrimitive()){
319-
return true;
320-
}
321-
return false;
316+
317+
protected boolean isCallSettersOnNulls(Class<?> propertyType) {
318+
return (configuration.isCallSettersOnNulls() && !propertyType.isPrimitive());
322319
}
323-
320+
324321
protected Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
325-
final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
326322
if (propertyMapping.getNestedQueryId() != null) {
327323
return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
328-
} else if (typeHandler != null) {
324+
} else if (propertyMapping.getNestedResultMapId() != null) {
325+
// the user added a column attribute to a nested result map, ignore it
326+
return OMIT;
327+
} else {
328+
final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
329+
if (typeHandler == null) { // issue #9
330+
throw new ExecutorException("Unknown type " + propertyMapping.getJavaType()
331+
+ " in mapping property=" + propertyMapping.getProperty()
332+
+ " column=" + propertyMapping.getColumn()
333+
+ ". You need to register a TypeHandler for this type for MyBatis to correctly convert the result.");
334+
}
329335
final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
330336
return typeHandler.getResult(rs, column);
331337
}
332-
return OMIT;
333338
}
334339

335340
protected boolean applyAutomaticMappings(ResultSet rs, List<String> unmappedColumnNames, MetaObject metaObject, String columnPrefix, ResultColumnCache resultColumnCache) throws SQLException {
@@ -353,7 +358,7 @@ protected boolean applyAutomaticMappings(ResultSet rs, List<String> unmappedColu
353358
final Object value = typeHandler.getResult(rs, columnName);
354359
if (value != null || isCallSettersOnNulls(propertyType)) { // issue #377, call setter on nulls
355360
metaObject.setValue(property, value);
356-
foundValues = true;
361+
foundValues = (value != null) || foundValues;
357362
}
358363
}
359364
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
unknownObject varchar(20)
23+
);
24+
25+
insert into users (id, name, unknownObject) values(1, 'User1', 'unknownObject');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
package org.apache.ibatis.submitted.unknownobject;
17+
18+
public interface Mapper {
19+
20+
User getUser(Integer id);
21+
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.unknownobject.Mapper">
22+
23+
<select id="getUser" resultMap="map">
24+
select * from users
25+
</select>
26+
27+
<resultMap type="org.apache.ibatis.submitted.unknownobject.User" id="map" autoMapping="false">
28+
<result column="id" property="id" />
29+
<result column="name" property="name" />
30+
<result column="unknownObject" property="unknownObject" />
31+
</resultMap>
32+
33+
</mapper>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.apache.ibatis.submitted.unknownobject;
2+
3+
public class UnknownObject {
4+
5+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
package org.apache.ibatis.submitted.unknownobject;
17+
18+
import java.io.Reader;
19+
import java.sql.Connection;
20+
21+
import org.apache.ibatis.exceptions.PersistenceException;
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.Assert;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class UnknownObjectTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeClass
36+
public static void setUp() throws Exception {
37+
// create a SqlSessionFactory
38+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/unknownobject/mybatis-config.xml");
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
reader.close();
41+
42+
// populate in-memory database
43+
SqlSession session = sqlSessionFactory.openSession();
44+
Connection conn = session.getConnection();
45+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/unknownobject/CreateDB.sql");
46+
ScriptRunner runner = new ScriptRunner(conn);
47+
runner.setLogWriter(null);
48+
runner.runScript(reader);
49+
reader.close();
50+
session.close();
51+
}
52+
53+
@Test(expected=PersistenceException.class)
54+
public void shouldGetAUser() {
55+
SqlSession sqlSession = sqlSessionFactory.openSession();
56+
try {
57+
Mapper mapper = sqlSession.getMapper(Mapper.class);
58+
User user = mapper.getUser(1);
59+
Assert.assertEquals("User1", user.getName());
60+
} finally {
61+
sqlSession.close();
62+
}
63+
}
64+
65+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package org.apache.ibatis.submitted.unknownobject;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
private UnknownObject unknownObject;
23+
24+
public UnknownObject getUnknownObject() {
25+
return unknownObject;
26+
}
27+
28+
public void setUnknownObject(UnknownObject unknownObject) {
29+
this.unknownObject = unknownObject;
30+
}
31+
32+
public Integer getId() {
33+
return id;
34+
}
35+
36+
public void setId(Integer id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
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-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:unknownobject" />
31+
<property name="username" value="sa" />
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
<mappers>
37+
<mapper resource="org/apache/ibatis/submitted/unknownobject/Mapper.xml" />
38+
</mappers>
39+
40+
</configuration>

0 commit comments

Comments
 (0)