emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

Global parameter typeHandler not invoked in case of null values #191

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Mybatis Version 3.0.3

We have a global type handler defined as follows:

<configuration>
...
    <typeHandlers>
        <typeHandler javaType="com.whatever.model.Context"
            handler="com.whatever.typeHandlers.ContextTypeHandler" />
    </typeHandlers>
...

</configuration>

This type handler is used by a select SQL in case the parameter is not null, 
however it's not invoked if the parameter is null - unless the type handler is 
(again) specified inline with the parameter in the mapper xml.

Example mapper xml snippet:

a) This does not work (the type handler is only invoked if "context" is not 
null)

<sql id="getTranslationsWhereClause">
 AND TRA_CONTEXT=#{context}
</sql>

b) this works as expected, the type handler is invoked in case the "context" 
parameter is null and not null.

<sql id="getTranslationsWhereClause">
 AND TRA_CONTEXT=#{context, typeHandler=com.whatever.model.ContextTypeHandler}
</sql>

We'd have expected that the globally defined type handler is used whenever a 
parameter of type "com.whatever.model.Context" is involved - even if the actual 
instance of the parameter is null.

Original issue reported on code.google.com by stoff...@gmail.com on 1 Dec 2010 at 12:57

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Look that there is not any way to know which parameter type was used with this:
<sql id="getTranslationsWhereClause">
 AND TRA_CONTEXT=#{context}
</sql>

And this call:
sqlSession.selectOne("xxx", null);

So it is simply impossible knowing what typehandler to use.

It should also work if you add a parameterType to your statement.

Original comment by eduardo.macarron on 17 Jan 2012 at 9:45