Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing TypeHandler returns null return than throwing an exception #9

Closed
voidmain opened this issue Mar 8, 2013 · 4 comments
Closed
Assignees
Labels
Milestone

Comments

@voidmain
Copy link

voidmain commented Mar 8, 2013

I run into an issue where I mapped a result in a resultMap to Joda's DateTime but didn't register a TypeHandler. Rather than failing with a new error message, it looks like the org.apache.ibatis.executor.resultset.FastResultSetHandler class is returning null in the getPropertyMappingValue method (somewhere near line 316).

This code should be more like this:

  protected Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
    final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
    if (propertyMapping.getNestedQueryId() != null) {
      return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
    } else if (typeHandler != null) {
      final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
      return typeHandler.getResult(rs, column);
    }
    throw new TypeHandlerException("Unknown type " + propertyMapping.getJavaType() + ". You need to register a TypeHandler for this type for MyBatis to correctly convert the result.");
  }

Instead, it looks like this (notice the return null at the end):

  protected Object getPropertyMappingValue(ResultSet rs, MetaObject metaResultObject, ResultMapping propertyMapping, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
    final TypeHandler<?> typeHandler = propertyMapping.getTypeHandler();
    if (propertyMapping.getNestedQueryId() != null) {
      return getNestedQueryMappingValue(rs, metaResultObject, propertyMapping, lazyLoader, columnPrefix);
    } else if (typeHandler != null) {
      final String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
      return typeHandler.getResult(rs, column);
    }
    return null;
  }
@emacarron
Copy link
Member

In orde to speed up the resolution, could you create a test for this?

http://code.google.com/p/mybatis/wiki/Test

@agustafson
Copy link
Member

Personally, if it wasn't possible to find an appropriate mapper for a type then I would expect it to throw an exception. In the case above, your date column would've been null and you would've had no idea why that was the case. Would it not be better to ensure that you have a type handler for every type that you've trying to retrieve?

@emacarron
Copy link
Member

Agreed.
That looks indeed a bug because if I don`t recall wrong there should be no mappings without a type handler. If the type is not know it should youse the UnknownTypeHandler. We should first check that.

@amitter77
Copy link

Hi, since I upgraded to mybatis 3.2.2 I have started seeing an issue that I think may be related to this change. I opened up the issue and it is described here: issue61

harawata added a commit to harawata/mybatis-3 that referenced this issue Jan 29, 2025
… account

Fail-fast is nice, but it practically prevents MyBatis from using runtime JDBC type information to resolve type handler.

Related to mybatis#9 mybatis#19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants