ResultMap gives no error even when mapping column does not exist

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

ResultMap gives no error even when mapping column does not exist

p_vo
I'm using MyBatis 3.5.0 and having a problem with ResultMap.

I have a simple ResultMap like below.

<resultMap type="Program" id="program">
    <result column="testColumn" property="testColumn" />
</resultMap>

<select id="findProgram" resultMap="program">
    SELECT test_column
    FROM PROGRAM_DATA
</select>

As you can see, the result from executing the SQL query
does not even include testColumn but no error was raised
when I tried to test the code. Because of that reason,
ResultMap cannot map any value to testColumn variable
and hence the code always return the default value of
that variable (in this case "false" because it is a 
boolean variable).

Is this behaviour intentional or is this a bug?
Thanks for any valuable input.

--
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: ResultMap gives no error even when mapping column does not exist

Guy Rouillier-2
On 3/12/2019 11:54:40 PM, [hidden email] wrote:

I'm using MyBatis 3.5.0 and having a problem with ResultMap.

I have a simple ResultMap like below.

<resultMap type="Program" id="program">
    <result column="testColumn" property="testColumn" />
</resultMap>

<select id="findProgram" resultMap="program">
    SELECT test_column
    FROM PROGRAM_DATA
</select>

As you can see, the result from executing the SQL query
does not even include testColumn but no error was raised
when I tried to test the code. Because of that reason,
ResultMap cannot map any value to testColumn variable
and hence the code always return the default value of
that variable (in this case "false" because it is a 
boolean variable).

Is this behaviour intentional or is this a bug?
Thanks for any valuable input.

Yes, this is normal behavior.  The result map does not need to contain all the columns in the query; unmapped columns are simply ignored.  The result map can also contain column mappings for columns not in the query; again, column mappings referencing columns not in the query are simply ignored.  You might be wondering why this approach was chosen.  A single mapping may be used for multiple queries.  You can define your result map to cover the maximum set of properties in the target class; but each query only needs to populate the properties it is interested in.  So, you get maximum functionality from your single mapping, and flexibility from the queries.  If you're new to MyBatis, another mapping feature you might find useful is autoMapping; this allows you to populate properties in your target class without having to list all of them in the result map.  As long as the properties have the same name as the columns, MyBatis will find them automatically; if can even handle automatically mapping to Java camelCase properties from underscore columns, e.g. column client_name -> Java property clientName.

--
Guy Rouillier



--
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: ResultMap gives no error even when mapping column does not exist

p_vo
Can this behaviour become an option instead? I think this potentially
creates bugs in any system if the developer get the wrong column name.
Even if you use autoMapping to map test_column into testColumn there
is no chance it can map testColum into testColumn.
Admittedly we did not cover this in our unit test but it would be a lot more
convenient if the risk is not there from the beginning.

On Wednesday, March 13, 2019 at 1:33:47 PM UTC+9, Guy Rouillier wrote:
On 3/12/2019 11:54:40 PM, <a href="javascript:" style="font-size:12pt" target="_blank" gdf-obfuscated-mailto="Qi10Wja6BQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">p_...@... wrote:

I'm using MyBatis 3.5.0 and having a problem with ResultMap.

I have a simple ResultMap like below.

<resultMap type="Program" id="program">
    <result column="testColumn" property="testColumn" />
</resultMap>

<select id="findProgram" resultMap="program">
    SELECT test_column
    FROM PROGRAM_DATA
</select>

As you can see, the result from executing the SQL query
does not even include testColumn but no error was raised
when I tried to test the code. Because of that reason,
ResultMap cannot map any value to testColumn variable
and hence the code always return the default value of
that variable (in this case "false" because it is a 
boolean variable).

Is this behaviour intentional or is this a bug?
Thanks for any valuable input.

Yes, this is normal behavior.  The result map does not need to contain all the columns in the query; unmapped columns are simply ignored.  The result map can also contain column mappings for columns not in the query; again, column mappings referencing columns not in the query are simply ignored.  You might be wondering why this approach was chosen.  A single mapping may be used for multiple queries.  You can define your result map to cover the maximum set of properties in the target class; but each query only needs to populate the properties it is interested in.  So, you get maximum functionality from your single mapping, and flexibility from the queries.  If you're new to MyBatis, another mapping feature you might find useful is autoMapping; this allows you to populate properties in your target class without having to list all of them in the result map.  As long as the properties have the same name as the columns, MyBatis will find them automatically; if can even handle automatically mapping to Java camelCase properties from underscore columns, e.g. column client_name -> Java property clientName.

--
Guy Rouillier



--
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: ResultMap gives no error even when mapping column does not exist

Guy Rouillier-2
On Wed, 2019-03-13 at 14:56 +0900, [hidden email] wrote:
Can this behaviour become an option instead? I think this potentially
creates bugs in any system if the developer get the wrong column name.
Even if you use autoMapping to map test_column into testColumn there
is no chance it can map testColum into testColumn.
Admittedly we did not cover this in our unit test but it would be a lot more
convenient if the risk is not there from the beginning.

Not sure which part you are referring to when you say "this behavior". You can always turn always set autoMapping = "false" at a global level, and then MyBatis will not do any auto mapping. The only columns that will be mapped to class properties are those you identify specifically in your result maps. But rereading your original post, that will still not generate an error in your situation. Take a look at autoMappingUnknownColumnBehavior. I've never used it, but perhaps it does what you are looking for.


On Wednesday, March 13, 2019 at 1:33:47 PM UTC+9, Guy Rouillier wrote:
On 3/12/2019 11:54:40 PM, <a href="javascript:" style="font-size:12pt" target="_blank" gdf-obfuscated-mailto="Qi10Wja6BQAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">p_...@... wrote:

I'm using MyBatis 3.5.0 and having a problem with ResultMap.

I have a simple ResultMap like below.

<
resultMap 
type=
"Program" 
id=
"program"
>
    <
result 
column=
"testColumn" 
property=
"testColumn" 
/>
</
resultMap
>

<
select 
id=
"findProgram" 
resultMap=
"program"
>
    SELECT test_column
    FROM PROGRAM_DATA
</
select
>

As you can see, the result from executing the SQL query
does not even include testColumn but no error was raised
when I tried to test the code. Because of that reason,
ResultMap cannot map any value to testColumn variable
and hence the code always return the default value of
that variable (in this case "false" because it is a 
boolean variable).

Is this behaviour intentional or is this a bug?
Thanks for any valuable input.

Yes, this is normal behavior.  The result map does not need to contain all the columns in the query; unmapped columns are simply ignored.  The result map can also contain column mappings for columns not in the query; again, column mappings referencing columns not in the query are simply ignored.  You might be wondering why this approach was chosen.  A single mapping may be used for multiple queries.  You can define your result map to cover the maximum set of properties in the target class; but each query only needs to populate the properties it is interested in.  So, you get maximum functionality from your single mapping, and flexibility from the queries.  If you're new to MyBatis, another mapping feature you might find useful is autoMapping; this allows you to populate properties in your target class without having to list all of them in the result map.  As long as the properties have the same name as the columns, MyBatis will find them automatically; if can even handle automatically mapping to Java camelCase properties from underscore columns, e.g. column client_name -> Java property clientName.

--
Guy Rouillier



--
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.
-- 
--
Guy Rouillier

--
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.