Skip to content

Commit 243cc67

Browse files
committed
Fixes #19 and #9. Fail fast when there is no typehandler for a mapping.
1 parent 86bde17 commit 243cc67

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,6 @@ protected Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObje
326326
return OMIT;
327327
} else {
328328
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-
}
335329
final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
336330
return typeHandler.getResult(rs, column);
337331
}
@@ -462,7 +456,7 @@ protected Object getNestedQueryConstructorValue(ResultSet rs, ResultMapping cons
462456
final Class<?> targetType = constructorMapping.getJavaType();
463457
final Object nestedQueryCacheObject = getNestedQueryCacheObject(nestedQuery, key);
464458
if (nestedQueryCacheObject != null && nestedQueryCacheObject instanceof List) {
465-
value = resultExtractor.extractObjectFromList((List<Object>)nestedQueryCacheObject, targetType);
459+
value = resultExtractor.extractObjectFromList((List<Object>) nestedQueryCacheObject, targetType);
466460
} else {
467461
final ResultLoader resultLoader = new ResultLoader(configuration, executor, nestedQuery, nestedQueryParameterObject, targetType, key, nestedBoundSql);
468462
value = resultLoader.loadResult();
@@ -471,7 +465,8 @@ protected Object getNestedQueryConstructorValue(ResultSet rs, ResultMapping cons
471465
return value;
472466
}
473467

474-
protected Object getNestedQueryMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
468+
protected Object getNestedQueryMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix)
469+
throws SQLException {
475470
final String nestedQueryId = propertyMapping.getNestedQueryId();
476471
final String property = propertyMapping.getProperty();
477472
final MappedStatement nestedQuery = configuration.getMappedStatement(nestedQueryId);
@@ -484,7 +479,7 @@ protected Object getNestedQueryMappingValue(ResultSet rs, MetaObject metaResultO
484479
final Class<?> targetType = propertyMapping.getJavaType();
485480
final Object nestedQueryCacheObject = getNestedQueryCacheObject(nestedQuery, key);
486481
if (nestedQueryCacheObject != null && nestedQueryCacheObject instanceof List) {
487-
value = resultExtractor.extractObjectFromList((List<Object>)nestedQueryCacheObject, targetType);
482+
value = resultExtractor.extractObjectFromList((List<Object>) nestedQueryCacheObject, targetType);
488483
} else if (executor.isCached(nestedQuery, key)) {
489484
executor.deferLoad(nestedQuery, metaResultObject, property, key, targetType);
490485
} else {

src/main/java/org/apache/ibatis/mapping/ResultMapping.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,29 @@ public Builder composites(List<ResultMapping> composites) {
117117
}
118118

119119
public ResultMapping build() {
120-
validate();
121120
// lock down collections
122121
resultMapping.flags = Collections.unmodifiableList(resultMapping.flags);
123122
resultMapping.composites = Collections.unmodifiableList(resultMapping.composites);
124123
resolveTypeHandler();
124+
validate();
125125
return resultMapping;
126126
}
127127

128128
private void validate() {
129-
// Issue 4: column is mandatory on nested queries
130-
if (resultMapping.nestedQueryId != null && resultMapping.column == null && resultMapping.composites.size() == 0) {
131-
throw new IllegalStateException("Missing column attribute for nested select in property " + resultMapping.property);
132-
}
133-
// Issue 697: cannot define both nestedQueryId and nestedResultMapId
129+
// Issue #697: cannot define both nestedQueryId and nestedResultMapId
134130
if (resultMapping.nestedQueryId != null && resultMapping.nestedResultMapId != null) {
135131
throw new IllegalStateException("Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.property);
136132
}
133+
// Issue #5: there should be no mappings without typehandler
134+
if (resultMapping.nestedQueryId == null && resultMapping.nestedResultMapId == null && resultMapping.typeHandler == null) {
135+
throw new IllegalStateException("No typehandler found for property " + resultMapping.property);
136+
}
137+
// Issue #4: column is mandatory on nested queries
138+
if (resultMapping.nestedQueryId != null && resultMapping.column == null && resultMapping.composites.size() == 0) {
139+
throw new IllegalStateException("Missing column attribute for nested select in property " + resultMapping.property);
140+
}
137141
}
138-
142+
139143
private void resolveTypeHandler() {
140144
if (resultMapping.typeHandler == null) {
141145
if (resultMapping.javaType != null) {

src/test/java/org/apache/ibatis/submitted/unknownobject/UnknownObjectTest.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424
import org.apache.ibatis.session.SqlSession;
2525
import org.apache.ibatis.session.SqlSessionFactory;
2626
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27-
import org.junit.Assert;
28-
import org.junit.BeforeClass;
2927
import org.junit.Test;
3028

3129
public class UnknownObjectTest {
3230

3331
private static SqlSessionFactory sqlSessionFactory;
3432

35-
@BeforeClass
36-
public static void setUp() throws Exception {
33+
@Test(expected=PersistenceException.class)
34+
public void shouldFailBecauseThereIsAPropertyWithoutTypeHandler() throws Exception {
3735
// create a SqlSessionFactory
3836
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/unknownobject/mybatis-config.xml");
3937
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
@@ -50,16 +48,4 @@ public static void setUp() throws Exception {
5048
session.close();
5149
}
5250

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-
6551
}

0 commit comments

Comments
 (0)