ResultHandler specification in mapper configuration

classic Classic list List threaded Threaded
7 messages Options
q8p
Reply | Threaded
Open this post in threaded view
|

ResultHandler specification in mapper configuration

q8p
We are using MyBatis, with spring integration and lazy loading. Now we
(think we) are running into a limitation in MyBatis: It is not
possible to -- configure -- a mapper with a result handler.

The current approach (what I've read in the user group) to relate a
result handler with a mapped statement is to pass it as an argument to
a SqlSession. This spoils all the magic when using spring and/or (lazy
loaded) collections.

Spoiling spring:
Spring integration provides a way to automatically register mappers in
the spring context. Users of a mapper can call mapped statements,
except when a 'special' result handler is needed; then the magic is
gone, because you need to inject the spring template and call the
mapper in a more 'native' manner.

Spoiling (lazy loaded) collections:
(Lazy loaded) collection provides a way chain mappers. The big
advantage is that mappers can be easily reused when there is a join
relation, except when a 'special' result handler is needed to process
the results from the 'collection' mapper; the magic is gone, because
the special mapper that is needed to do some special processing is not
used when loading the collection data (there is no way to do that,
because the chain is specified -- configuration time --, and not
runtime.

Discussion topic: Result handlers should be declared during
configuration

Why? I most cases where you want a specific handler (eg. to relate
parents and childs), you always what to use that handler. A
specification in the mapper XML (or annotation) prevents users of that
mapper to forget to use the right handler.

What's next? As I can see now, a couple of changes are needed to
support this:
- Add a 'resultHandlerType' to the resultMap configuration.
- This should be used when no specific result handler is passed at
runtime
- The 'resultHandlerType' of the mapper referred to in a 'collection'
should be used correctly to build the result collection.
- (bonus) Add a MyBatis defaultResultHandlerType to configure the
default handler type (and use DefaultResultHandler when it is not
specified). This allows users to use a different type if List as a
default.

Let's have an open discussion on this topic, so we come up with a good
solution...
q8p
Reply | Threaded
Open this post in threaded view
|

Re: ResultHandler specification in mapper configuration

q8p
I've started to make a patch for this, but I would like to have some
feedback on this idea.

Also one question while creating the patch: My proposal involves an
update on the mapper dtd (to add the resultHandlerType to resultMap).
Currently it points to 'http://ibatis.apache.org/dtd/ibatis-3-
mapper.dtd', but a new version is needed. What / who is involved in
that?
q8p
Reply | Threaded
Open this post in threaded view
|

Re: ResultHandler specification in mapper configuration

q8p
To one of the committers: The development of this patch (especially
the unit tests) cannot proceed, because we need to know how to put a
'3.1' mapper dtd on mybatis.org

(We really need this functionality, but we cannot implement the patch
without your support. Feedback on this proposal/patch is also very
welcome)
Reply | Threaded
Open this post in threaded view
|

Re: ResultHandler specification in mapper configuration

Clinton Begin
Administrator
Not that I'm opposed to doing so... but you do have options.

*  Your IDE should allow local resolution of DTDs.

* You can use a file:/// reference for the DTD in your XML doc (just for testing)

* If you build MyBatis using the Maven POMs etc., it's configured by default to use an internal resolver that loads the DTD from the JAR file anyway. 

This is how we code changes to the DTD without prematurely releasing it to the web.

Cheers,
Clinton

On Wed, Apr 20, 2011 at 2:18 PM, q8p <[hidden email]> wrote:
To one of the committers: The development of this patch (especially
the unit tests) cannot proceed, because we need to know how to put a
'3.1' mapper dtd on mybatis.org

(We really need this functionality, but we cannot implement the patch
without your support. Feedback on this proposal/patch is also very
welcome)

q8p
Reply | Threaded
Open this post in threaded view
|

Re: ResultHandler specification in mapper configuration

q8p
Thanks! I was afraid that the maven build couldn't run my tests, but
it worked without a problem.

I've created a patch and attached is with issue 312:
http://code.google.com/p/mybatis/issues/detail?id=312

Please let me know what you think and (when it's acceptable) when it
can be integrated. I'll also create a patch for the documentation when
this is ok. See below a summary of the patch:

- 'resultHandlerType' attribute on the 'resultMap' element to support
xml configuration
- '@ResultHandlerType' annotation to support annotation configuration
- 'defaultListResultHandlerType' configuration setting to set a
default result handler

Unit tests added in:
- org.apache.ibatis.submitted.resulthandlertype.*Test
- org.apache.ibatis.builder.XmlConfigBuilderTest
Reply | Threaded
Open this post in threaded view
|

Re: ResultHandler specification in mapper configuration

Clinton Begin
Administrator
Thanks you!  That does look like a good patch.  

Cheers,
Clinton

On Thu, Apr 21, 2011 at 7:29 AM, q8p <[hidden email]> wrote:
Thanks! I was afraid that the maven build couldn't run my tests, but
it worked without a problem.

I've created a patch and attached is with issue 312:
http://code.google.com/p/mybatis/issues/detail?id=312

Please let me know what you think and (when it's acceptable) when it
can be integrated. I'll also create a patch for the documentation when
this is ok. See below a summary of the patch:

- 'resultHandlerType' attribute on the 'resultMap' element to support
xml configuration
- '@ResultHandlerType' annotation to support annotation configuration
- 'defaultListResultHandlerType' configuration setting to set a
default result handler

Unit tests added in:
- org.apache.ibatis.submitted.resulthandlertype.*Test
- org.apache.ibatis.builder.XmlConfigBuilderTest

q8p
Reply | Threaded
Open this post in threaded view
|

Re: ResultHandler specification in mapper configuration

q8p
Any idea when this will be integrated? I've also suggested an
improvement (see note 5 at issue 312)