How to make Mapper to sql query

1,244 views
Skip to first unread message

nonameplum

unread,
Oct 7, 2013, 12:58:10 PM10/7/13
to jd...@googlegroups.com
Hi,
(I'm new to java world)
I'm trying to make interface that uses generic mapper for sql query

Example:

    public interface SomeQueries
    {
        @SqlQuery("select * from SOMETABLE")
        @Mapper(MapMappper.class)
        public List<Map<String, Object>> sqlQuery();
    }

    public class MapMappper implements ResultSetMapper<List<Map<String, Object>>> {

        @Override
        public List<Map<String, Object>> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
            System.out.println(r);
            Map<String, Object> obj = new HashMap<String, Object>();
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

            list.add(obj);
            return list;
        }
    }

But I'm getting exceptions:
Caused by: org.skife.jdbi.v2.exceptions.UnableToCreateStatementException: unable to access mapper
at org.skife.jdbi.v2.sqlobject.ResultReturnThing.map(ResultReturnThing.java:27)
at org.skife.jdbi.v2.sqlobject.QueryHandler.invoke(QueryHandler.java:45)
at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:147)
at org.skife.jdbi.v2.sqlobject.SqlObject$1.intercept(SqlObject.java:60)
at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass$$EnhancerByCGLIB$$cf0a6337.sqlQuery(<generated>)
at sample.Controller.getTowars(Controller.java:77)
... 57 more
Caused by: java.lang.InstantiationException: sample.Controller$MapMappper
at java.lang.Class.newInstance(Class.java:417)
at org.skife.jdbi.v2.sqlobject.ResultReturnThing.map(ResultReturnThing.java:24)
... 62 more
Caused by: java.lang.NoSuchMethodException: sample.Controller$MapMappper.<init>()
at java.lang.Class.getConstructor0(Class.java:2871)
at java.lang.Class.newInstance(Class.java:402)
... 63 more

Steven Schlansker

unread,
Oct 7, 2013, 1:40:15 PM10/7/13
to jd...@googlegroups.com
On Oct 7, 2013, at 9:58 AM, nonameplum <sliwins...@gmail.com> wrote:
>
> Example:
> <snip>
>
> public class MapMappper implements ResultSetMapper<List<Map<String, Object>>> {
>
> @Override
> public List<Map<String, Object>> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
> System.out.println(r);
> Map<String, Object> obj = new HashMap<String, Object>();
> List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
>
> list.add(obj);
> return list;
> }
> }
>
> But I'm getting exceptions:
>
> Caused by: java.lang.NoSuchMethodException: sample.Controller$MapMappper.<init>()
> at java.lang.Class.getConstructor0(Class.java:2871)
> at java.lang.Class.newInstance(Class.java:402)
> ... 63 more

Are you sure the code you pasted here is exactly as it is in the source file? The most common cause for this specific problem is having your mapper as a non-static nested class, e.g.

public class MyDao {
public class MyMapper { … }
}

The problem is that in Java a inner class (a non-static nested class) has access to members of the enclosing object. To allow this access, the inner class has a hidden reference to the outer class, something like:

public class MyDao {
public class MyMapper {
private final MyDao outerReference;
public MyMapper(MyDao outerReference) { this.outerReference = outerReference; }
}
}

JDBI doesn't understand this "hidden" constructor parameter, and attempts to access a constructor with no parameters (the "sample.Controller$MapMappper.<init>()" bit), which does not exist.

TL;DR version: make your mapper static, or show us the actual code with more context.

nonameplum

unread,
Oct 7, 2013, 3:21:08 PM10/7/13
to jd...@googlegroups.com
Thanks, that was the problem.
Reply all
Reply to author
Forward
0 new messages