Type Handler for Single Primitive Result Type

592 views
Skip to first unread message

Truth

unread,
Feb 21, 2011, 5:21:30 PM2/21/11
to mybatis-user
We're trying to specify a Type Handler for a single primitive result,
without overriding the default type handler for all primitives of that
type.

For example, for the following style select statement, we want to
specify the type handler to be used:
<select resultType="boolean">

Our current work around is to wrap it in a simple class, and use a
result map - but this breaks our API.

Can someone point out how this is done?

Poitras Christian

unread,
Feb 22, 2011, 8:39:22 AM2/22/11
to mybati...@googlegroups.com
Try to use a resultMap.

<resultMap id="special_boolean" type="boolean">
<constructor>
<arg column="your_column" typeHandler="YourTypeHandler"/>
</constructor>
</resultMap>

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Truth
Envoyé : February-21-11 5:22 PM
À : mybatis-user
Objet : Type Handler for Single Primitive Result Type

Truth

unread,
Mar 27, 2011, 7:05:07 PM3/27/11
to mybati...@googlegroups.com
Sorry it's taken us so long on this - things with workarounds tend to
get put lower priority.

The suggested solution didn't work for us, we always got a false
response regardless of input.
Throwing some debug tracing on, it seems with the resultMap type of
Boolean the typehandler we specify never gets called.

<resultMap id="special_boolean" type="Boolean">
<constructor>
<arg column="our_column" javaType="_boolean" typeHandler="OurTypeHandler"/>
</constructor>
</resultMap>

However on our own customer class, the typehandler still gets called:

<resultMap id="special_boolean" type="BooleanContainer">
<constructor>
<arg column="our_column" javaType="_boolean" typeHandler="OurTypeHandler"/>
</constructor>
</resultMap>

Is there some sort for automatic handler for a resultMap type of
Boolean that we cannot override?

--
Truth.

On 23 February 2011 02:39, Poitras Christian

Poitras Christian

unread,
Mar 28, 2011, 8:35:02 AM3/28/11
to mybati...@googlegroups.com
It's possible.

You can try either
SqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(Boolean.TYPE, YourTypeHandler);
Or register a JdbcType that match your data like
sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(JdbcType.BIT, YourTypeHandler);
I haven't tried that for boolean.

You may also prefer to have a private special getter/setter/constructor that doesn't use boolean but another type that is easier to deal with.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Truth

Envoyé : March-27-11 7:05 PM
À : mybati...@googlegroups.com
Objet : Re: Type Handler for Single Primitive Result Type

Truth

unread,
Mar 28, 2011, 4:58:53 PM3/28/11
to mybati...@googlegroups.com
Yes, but as in the OP, we don't want to change it for any other
Boolean's we're fetching - just this one.

Theoretically your previous suggestion should work, however when
MyBatis sees the resultMap type of Boolean, it doesn't process the
rest of the resultMap. It would appear the default handler for Boolean
overrides any custom resultMap specified for it.

The alternative I would have hoped for was the ability to set a
typeHandler for the result of a select where a resultType has been
specified.

At the moment for a work around, we've created our own
BooleanContainer custom type which we handling as per a normal
JavaBean - and inserting a dummy DAO layer between the actual DAO and
the Service call - that unwraps this BooleanContainer and returns a
boolean to match the API spec. However it seems a hack to hack to do
so, and the additional (minimal) overhead of another Bean being
managed.


Unless I've missed something it seems the default overriding the
resultMap for Boolean is a bug. And the lack of ability to specify a
typeHandler for a single result from a select statement a missing gap
in our functionality.

--
Truth.

lwpro

unread,
Mar 28, 2011, 10:46:49 PM3/28/11
to mybati...@googlegroups.com
Hi,

Just out of curiosity, what do you need a typehandler for a boolean type?

Cheers,
Jackie

Truth

unread,
Mar 28, 2011, 10:57:12 PM3/28/11
to mybati...@googlegroups.com
Because in this case the values being stored are 'N' and 'Y'.
Existing database, and attached applications - so we can't change the
existing data.

It's somewhat annoying, but I was expecting that MyBatis would have a
simple solution. Or at least simpler than our current work-around.

--
Truth.

lwpro

unread,
Mar 28, 2011, 11:02:13 PM3/28/11
to mybati...@googlegroups.com
if that's the case, you can achieve it by modifying your query.

select (flag = false) newFlag from myTable;

Cheers,
Jackie

Truth

unread,
Mar 28, 2011, 11:11:59 PM3/28/11
to mybati...@googlegroups.com
If I put anything inside brackets in the column expression part of the
select query, I get an SQL error (Incorrect syntax).
I'm not quite sure what else you're trying to refer to here.

--
Truth.

lwpro

unread,
Mar 28, 2011, 11:30:54 PM3/28/11
to mybati...@googlegroups.com
Hi,

I didn't run the query myself,
select (flag = false) newFlag from myTable;

Actually I am just trying to suggest to manipulate the query to negate the boolean value at database level, instead of using MyBatis typeHandler.

I think for oracle, we can use decode, or case function to negate the value, for example,
select case when group_id= 2 then 'true' else 'false' end as flag  from GTP_Group;
select decode (group_id, 2, 'true', 3, 'false') as flag from GTP_Group;
however, as you might know, if for oracle, actually it doesn't support boolean datatype.

I hope this clarifies. Sorry for the misunderstanding.

Cheers,
Jackie

Truth

unread,
Mar 28, 2011, 11:40:07 PM3/28/11
to mybati...@googlegroups.com
Hmm, I see what you mean. That's doable. Might be better than what
we've got - would need to test.

But I still think there should be a way of doing this, as is.

--
Truth.

lwpro

unread,
Mar 28, 2011, 11:43:50 PM3/28/11
to mybati...@googlegroups.com
Great if it would help.

I also think it's reasonable to have such functionality, enabling customised typeHandler for single return type. However, seems at least it's not common. And hope others could comment if there really is.

Cheers,
Jackie

Poitras Christian

unread,
Mar 29, 2011, 9:25:23 AM3/29/11
to mybati...@googlegroups.com
As a side note:
You can specify a custom type handler for a single <result> easily even for Boolean, but it's much more difficult for a <resultMap>.

If you can set the Boolean as a <result> of another class, it would be very easy.
<resultMap id="someMap" type="SomeClass">
<result property="my_boolean" column="our_column" typeHandler="OurTypeHandler"/>
</resultMap>

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Truth

Envoyé : March-28-11 4:59 PM

lwpro

unread,
Mar 29, 2011, 9:32:03 AM3/29/11
to mybati...@googlegroups.com
Hi Christian,

I agree. Actually I think (s)he has been using a BooleanContainer, which is another form of wrapper already. It for sure works.

However, it seems there might be a way to create custom typeHandler over single return result directly. Not sure if anyone have experience on this.

Cheers,
Jackie

Poitras Christian

unread,
Mar 29, 2011, 10:13:37 AM3/29/11
to mybati...@googlegroups.com

Maybe there is. I’ve never done this myself, so I’m not sure.

 

As for your suggestion to modify the select output, this is what I use to convert values like ‘N’ and ‘Y’ to boolean.

 

Christian

 

De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de lwpro
Envoyé : March-29-11 9:32 AM

Reply all
Reply to author
Forward
0 new messages