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

Does not work Cursor using annotation based Mapper #661

Closed
kazuki43zoo opened this issue Apr 23, 2016 · 4 comments
Closed

Does not work Cursor using annotation based Mapper #661

kazuki43zoo opened this issue Apr 23, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Apr 23, 2016

MyBatis version

3.4.0

Database vendor and version

Any

Test case or example project

N/A

Steps to reproduce

At first, i add method in BoundBlogMapper as follow:

  @Select({
          "SELECT *",
          "FROM blog",
          "ORDER BY id"
  })
  Cursor<Blog> selectCursorBlogs();

At second, i add test method in BindingTest as follow:

  @Test
  public void shouldExecuteWithCursorAndRowBounds() {
    SqlSession session = sqlSessionFactory.openSession();
    try {
      BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
      Cursor<Blog> blogs = mapper.selectCursorBlogs();
      for (Blog b :blogs) {
        System.out.println(b);
      }
    } finally {
      session.close();
    }
  }

Expected result

Blog: 1 : Jim Business (null)
Blog: 2 : Bally Slog (null)

Actual result

org.apache.ibatis.reflection.ReflectionException: Error instantiating interface org.apache.ibatis.cursor.Cursor with invalid types () or values (). Cause: java.lang.NoSuchMethodException: org.apache.ibatis.cursor.Cursor.<init>()

    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:90)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:50)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:42)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:594)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:571)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:380)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:339)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:314)
    at org.apache.ibatis.cursor.defaults.DefaultCursor.fetchNextObjectFromDatabase(DefaultCursor.java:142)
    at org.apache.ibatis.cursor.defaults.DefaultCursor.fetchNextUsingRowBound(DefaultCursor.java:128)
    at org.apache.ibatis.cursor.defaults.DefaultCursor$CursorIterator.hasNext(DefaultCursor.java:199)
    at org.apache.ibatis.binding.BindingTest.shouldExecuteWithCursorAndRowBounds(BindingTest.java:774)
...
Caused by: java.lang.NoSuchMethodException: org.apache.ibatis.cursor.Cursor.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:62)
@kazuki43zoo
Copy link
Member Author

kazuki43zoo commented Apr 23, 2016

I've modified the MapperAnnotationBuilder#getReturnType(Method) (L.393) as follow:

//      if (Collection.class.isAssignableFrom(rawType)) { ← ### Original code ###
      if (Iterable.class.isAssignableFrom(rawType)) { // ← ### Changed code ###
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        if (actualTypeArguments != null && actualTypeArguments.length == 1) {
          Type returnTypeParameter = actualTypeArguments[0];
          if (returnTypeParameter instanceof Class<?>) {
            returnType = (Class<?>) returnTypeParameter;
          } else if (returnTypeParameter instanceof ParameterizedType) {
            // (gcode issue #443) actual type can be a also a parameterized type
            returnType = (Class<?>) ((ParameterizedType) returnTypeParameter).getRawType();
          } else if (returnTypeParameter instanceof GenericArrayType) {
            Class<?> componentType = (Class<?>) ((GenericArrayType) returnTypeParameter).getGenericComponentType();
            // (gcode issue #525) support List<byte[]>
            returnType = Array.newInstance(componentType, 0).getClass();
          }
        }

This code was worked. What do you think ?

@emacarron
Copy link
Member

I would check especifically for Cursor instead of for iterable.

2016-04-23 17:57 GMT+02:00 Kazuki Shimizu [email protected]:

I've modified the MapperAnnotationBuilder#getReturnType(Method) (L.393)
as follow:

// if (Collection.class.isAssignableFrom(rawType)) { ← ### Original code ###
if (Collection.class.isAssignableFrom(rawType)
|| Iterable.class.isAssignableFrom(rawType)) { // ← ### Added condition ###
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
if (actualTypeArguments != null && actualTypeArguments.length == 1) {
Type returnTypeParameter = actualTypeArguments[0];
if (returnTypeParameter instanceof Class) { returnType = (Class) returnTypeParameter;
} else if (returnTypeParameter instanceof ParameterizedType) {
// (gcode issue #443) actual type can be a also a parameterized type
returnType = (Class) ((ParameterizedType) returnTypeParameter).getRawType(); } else if (returnTypeParameter instanceof GenericArrayType) { Class componentType = (Class<?>) ((GenericArrayType) returnTypeParameter).getGenericComponentType();
// (gcode issue #525) support List<byte[]>
returnType = Array.newInstance(componentType, 0).getClass();
}
}

This code was worked. What do you think ?


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#661 (comment)

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Apr 23, 2016
emacarron added a commit that referenced this issue Apr 23, 2016
…on-based-mapper

Support Cursor on annotation based Mapper interface #661
@kazuki43zoo
Copy link
Member Author

please add milestone!!

@emacarron emacarron added the bug label Apr 23, 2016
@emacarron emacarron added this to the 3.4.1 milestone Apr 23, 2016
@emacarron emacarron self-assigned this Apr 23, 2016
@emacarron
Copy link
Member

Thanks for pointing @kazuki43zoo !

pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
…annotaion-based-mapper

Support Cursor on annotation based Mapper interface mybatis#661
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

2 participants