test="#{foo} != null" always evaluates True

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

test="#{foo} != null" always evaluates True

gfrancis1@gmail.com
Using Mybatis 3, I am passing a Map as parameters into the statement:

  <select id="selectReportableForUser"
parameterType="java.util.HashMap"
resultType="com.mycompany.myproduct.model.Level">
    select level_id, strata_id, level_name
    from public.level
    <if test="#{parent_level} != null">
                where level_id= #{parent_level}
            </if>
  </select>

If I pass in a Map with no entry for "parent_level" this executes
fine, with the following debug appearing in the console:
25 Oct 2010 13:21:05,716 DEBUG [PreparedStatement] ==>  Executing:
select level_id, strata_id, level_name from public.level
where level_id= ?
25 Oct 2010 13:21:05,716 DEBUG [PreparedStatement] ==> Parameters:
null

The conditional IF clause must evaluate true (since the where clause
is included in the query.
But, as shown in the parameters debug, the value is null (I have not
put a value for 'parent_level' in the parameter map)
So, is 'parent_level' null or not null according to myBatis?  Why does
the condition evaluate true?
Reply | Threaded
Open this post in threaded view
|

RE: test="#{foo} != null" always evaluates True

Poitras Christian
Try to use <if test="parent_level != null">

Christian

-----Message d'origine-----
De : [hidden email] [mailto:[hidden email]] De la part de blackfrancis
Envoyé : October-29-10 3:04 PM
À : mybatis-user
Objet : test="#{foo} != null" always evaluates True

Using Mybatis 3, I am passing a Map as parameters into the statement:

  <select id="selectReportableForUser"
parameterType="java.util.HashMap"
resultType="com.mycompany.myproduct.model.Level">
    select level_id, strata_id, level_name
    from public.level
    <if test="#{parent_level} != null">
                where level_id= #{parent_level}
            </if>
  </select>

If I pass in a Map with no entry for "parent_level" this executes
fine, with the following debug appearing in the console:
25 Oct 2010 13:21:05,716 DEBUG [PreparedStatement] ==>  Executing:
select level_id, strata_id, level_name from public.level
where level_id= ?
25 Oct 2010 13:21:05,716 DEBUG [PreparedStatement] ==> Parameters:
null

The conditional IF clause must evaluate true (since the where clause
is included in the query.
But, as shown in the parameters debug, the value is null (I have not
put a value for 'parent_level' in the parameter map)
So, is 'parent_level' null or not null according to myBatis?  Why does
the condition evaluate true?