How to map a single object to multiple columns using the TypeHandler

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

How to map a single object to multiple columns using the TypeHandler

Hooman Valibeigi
Hi,

Is it possible to map a single Object property of a class to multiple columns using the TypeHandler interface?
Consider the following example:

<resultMap id="Data" type="com.mycompany.Data">
    <id property="id" column="id"/>
    <result property="value" column="value" typeHandler="DataValueTypeHandler"/>
</resultMap>

instances of class Data with properties int id and Object value need to be mapped to 3 columns, in fact the value property need to be mapped to columns "value" and "valueType".
The problem is that the setParameter method of the TypeHandler interface does not take multiple column indices so I'm not sure whether this is even a genuine approach.
What are your thoughts?

Note: I know I can add an explicit property valueType to Data class and map it explicitly in the resultMap, but this is not my requirement.

Thank you for your useful ideas in advance

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to map a single object to multiple columns using the TypeHandler

Guy Rouillier-2
If you read the MyBatis User Guide, TypeHandlers are meant to handle the mapping of a javaType to a jdbcType in a statement independent way.  You can define the TypeHandler once, then reuse it any number of times, in any number of SQL statements, in any parameter position.

What you describe, as I understand it, would seem to eliminate one of the primary benefits, by tying the mapping to a particular statement.  To reuse your example, say we are talking about an insert statement.  When the TypeHandler gets invoked with the value property, you need to know apriori that the column index passed in represents the value column, while index+1 represents valueType.  Theoretically, you can do this in the setParameter method; nothing restricts you from calling:

   pStmt.setString(iIndex, ...);
   pStmt.setInt(iIndex+1, ...);

But you've then tightly connected that TypeHandler to that one insert statement, losing the benefit of a general type handler.

Maybe an interceptor would work better here.  See the User Guide for a description.  Or perhaps a database trigger on the value column, which would then update the valueType column.

--
Guy Rouillier

------ Original Message ------
From: "Hooman Valibeigi" <[hidden email]>
To: "mybatis-user" <[hidden email]>
Sent: 8/26/2017 11:53:55 AM
Subject: How to map a single object to multiple columns using the TypeHandler

Hi,

Is it possible to map a single Object property of a class to multiple columns using the TypeHandler interface?
Consider the following example:

<resultMap id="Data" type="com.mycompany.Data">
    <id property="id" column="id"/>
    <result property="value" column="value" typeHandler="DataValueTypeHandler"/>
</resultMap>

instances of class Data with properties int id and Object value need to be mapped to 3 columns, in fact the value property need to be mapped to columns "value" and "valueType".
The problem is that the setParameter method of the TypeHandler interface does not take multiple column indices so I'm not sure whether this is even a genuine approach.
What are your thoughts?

Note: I know I can add an explicit property valueType to Data class and map it explicitly in the resultMap, but this is not my requirement.

Thank you for your useful ideas in advance

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.