You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Paul Spencer <pa...@apache.org> on 2010/02/03 14:36:18 UTC

How to throw an exception when a property from a parameter map is null or missing?

Is there a way to throws an exception if an property from the input  
parameter evaluates to null in iBatis 3.0?

I have a generic query engine that accepts iBatis configuration files  
and a parameter map as input.  As a result the query engine only  
passes the parameter map to iBatis without knowing what properties in  
the map are required.  When a property is null or missing iBatis may  
throw a parsing exception because it does not know the datatype of the  
property, the DBMS server may throw an sql query parsing exception, or  
the query will execute producing unexpected results. Ideally I would  
like to use a mode in the query parameter, like  
#{col2Value,nonNullValue=required}, that would cause iBatis to throw  
an exception when the property evaluated to null.

As an example, I would like iBatis to throw an exception, like  
"MissingParameterException", when the property "col2Value" is null or  
not in the parameter map.

   <mapper namespace="com.foo.example">
     <select id="sampleQuery" parameterType="Map" resultType="Map">
         select column_1 from foo where column_2 = #{col2Value}
     </select>
   </mapper>


Paul Spencer

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: How to throw an exception when a property from a parameter map is null or missing?

Posted by Simone Tripodi <si...@gmail.com>.
Hi,
personally I totally agree with Clinton, generally speaking it should
be a task of a higher level of applications, validations seems easy
but it could become so complex that trying to mix it with the
persistence layer could be painful; also Hibernate guys have been
tried but at the end they created 2 different layers, the validation
one and the persistence one.

By the way, there is a JSR specification that takes care about Bean
Validation, the 303[1], and the Hibernate Validator[2] is the only one
production-ready implementation that I know, and you can use it
without any other hibernate dependency.

Hope this helps,
Simo

[1] http://jcp.org/en/jsr/detail?id=303
[2] https://www.hibernate.org/412.html - see the 4.0.2.GA release

http://people.apache.org/~simonetripodi/



On Wed, Feb 3, 2010 at 5:49 PM, Guy Rouillier <gu...@burntmail.com> wrote:
> On 2/3/2010 8:36 AM, Paul Spencer wrote:
>
>> Ideally I would like to use a
>> mode in the query parameter, like #{col2Value,nonNullValue=required},
>> that would cause iBatis to throw an exception when the property
>> evaluated to null.
>
> If you are willing to impose a clause like that above, then just require
> something similar in a comment preceding the statement in the mapper file.
>  Then your generic engine can look for such comments and apply the
> constraints before invoking the query.  Something like this:
>
>    <!-- @NotNull=col2Value, col3Value -->
>    <select id="sampleQuery" parameterType="Map" resultType="Map">
>        select column_1 from foo where column_2 = #{col2Value}
>    </select>
>
> --
> Guy Rouillier
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: How to throw an exception when a property from a parameter map is null or missing?

Posted by Guy Rouillier <gu...@burntmail.com>.
On 2/3/2010 8:36 AM, Paul Spencer wrote:

> Ideally I would like to use a
> mode in the query parameter, like #{col2Value,nonNullValue=required},
> that would cause iBatis to throw an exception when the property
> evaluated to null.

If you are willing to impose a clause like that above, then just require 
something similar in a comment preceding the statement in the mapper 
file.  Then your generic engine can look for such comments and apply the 
constraints before invoking the query.  Something like this:

     <!-- @NotNull=col2Value, col3Value -->
     <select id="sampleQuery" parameterType="Map" resultType="Map">
         select column_1 from foo where column_2 = #{col2Value}
     </select>

-- 
Guy Rouillier

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: How to throw an exception when a property from a parameter map is null or missing?

Posted by Clinton Begin <cl...@gmail.com>.
This enters a very big topic of validations in the persistence layer, which
I would approach very carefully and not simply add a few attributes here and
there.  I believe this should be done at a higher level in your
application.  While it may be convenient to piggyback on the iBATIS
configuration files, it's not necessarily the best way to go.

Clinton

On Wed, Feb 3, 2010 at 6:36 AM, Paul Spencer <pa...@apache.org> wrote:

> Is there a way to throws an exception if an property from the input
> parameter evaluates to null in iBatis 3.0?
>
> I have a generic query engine that accepts iBatis configuration files and a
> parameter map as input.  As a result the query engine only passes the
> parameter map to iBatis without knowing what properties in the map are
> required.  When a property is null or missing iBatis may throw a parsing
> exception because it does not know the datatype of the property, the DBMS
> server may throw an sql query parsing exception, or the query will execute
> producing unexpected results. Ideally I would like to use a mode in the
> query parameter, like #{col2Value,nonNullValue=required}, that would cause
> iBatis to throw an exception when the property evaluated to null.
>
> As an example, I would like iBatis to throw an exception, like
> "MissingParameterException", when the property "col2Value" is null or not in
> the parameter map.
>
>  <mapper namespace="com.foo.example">
>    <select id="sampleQuery" parameterType="Map" resultType="Map">
>        select column_1 from foo where column_2 = #{col2Value}
>    </select>
>  </mapper>
>
>
> Paul Spencer
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>