Problem iterating over a list of beans

View: New views
4 Messages — Rating Filter:   Alert me  

Problem iterating over a list of beans

by Todd Wilkinson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello-
I'm new to iBATIS and I'm having trouble with the <iterate> element.

I want to create a query for an entirely dynamic where clause.  I've
constructed a Bean called QueryCondition with the following properties:

String condition
String operator
String value

(along with public accessors and mutators)

I'm passing an ArrayList into the following mapping:

  <select id="myObject.dynamicQuery" resultMap="myObject_resultmap"
resultClass="java.util.List">
    select id, name, data, notes
    from objectTable
    <dynamic prepend="where">
                <iterate conjunction="and">
                        $[].condition$ $[].operator$ #[].value#
                </iterate>
        </dynamic>
  </select>

But it's not working.  In the logs, I see the following:
Preparing Statement:      select id, name, source_code_location, context,
notes     from objectTable where       my.package.QueryCondition@1f5b4d1
my.package.QueryCondition@1f5b4d1 ?

and then I get a logged exception:
0com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the sites.dynamicQuery-InlineParameterMap.
--- Check the parameter mapping for the '[0].value' property.
--- Cause: java.lang.NullPointerException
[full stack trace available if needed]
The line 'check the parameter mapping...' will occur for whichever one of
the properties a place in hashmarks, ie - #[].operator# will cause it
also.

I have verified that there is 1 QueryCondition object in this list which
has its properties set.  Strange that the prepared statement resolves the
first two properties to objects of type QueryCondition, instead of
property string valyes.  I guess I'm not understanding how to properly
access the objects in the list.  If I set a 'property' attribute in the
<iterate> tag,  I get:
--- The error occurred while preparing the mapped statement for execution.
--- Check the myObject.dynamicQuery.
--- Check the parameter map.
--- Cause: com.ibatis.common.beans.ProbeException: Error getting ordinal
list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException:
String index out of range: -1

Thanks for any help!


RE: Problem iterating over a list of beans

by Chris O'Connell-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm certainly not an expert at this, but what if instead of passing the list
right into the query, you created a map and put the list into the map with
the key "conditions" (then pass the map into the query).

Then, put something like this in the mapping:

<iterate property="conditions" conjunction="and">
        $conditions[].condition$ $conditions[].operator$ #[].value#
</iterate>

That is usually how I iterate over stuff.  Not sure if I am understanding
entirely what is going on with your situation, though...

Chris

-----Original Message-----
From: Todd Wilkinson [mailto:lp@...]
Sent: Tuesday, July 08, 2008 3:53 PM
To: user-java@...
Subject: Problem iterating over a list of beans


Hello-
I'm new to iBATIS and I'm having trouble with the <iterate> element.

I want to create a query for an entirely dynamic where clause.  I've
constructed a Bean called QueryCondition with the following properties:

String condition
String operator
String value

(along with public accessors and mutators)

I'm passing an ArrayList into the following mapping:

  <select id="myObject.dynamicQuery" resultMap="myObject_resultmap"
resultClass="java.util.List">
    select id, name, data, notes
    from objectTable
    <dynamic prepend="where">
                <iterate conjunction="and">
                        $[].condition$ $[].operator$ #[].value#
                </iterate>
        </dynamic>
  </select>

But it's not working.  In the logs, I see the following:
Preparing Statement:      select id, name, source_code_location, context,
notes     from objectTable where       my.package.QueryCondition@1f5b4d1
my.package.QueryCondition@1f5b4d1 ?

and then I get a logged exception:
0com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the sites.dynamicQuery-InlineParameterMap.
--- Check the parameter mapping for the '[0].value' property.
--- Cause: java.lang.NullPointerException
[full stack trace available if needed]
The line 'check the parameter mapping...' will occur for whichever one of
the properties a place in hashmarks, ie - #[].operator# will cause it
also.

I have verified that there is 1 QueryCondition object in this list which
has its properties set.  Strange that the prepared statement resolves the
first two properties to objects of type QueryCondition, instead of
property string valyes.  I guess I'm not understanding how to properly
access the objects in the list.  If I set a 'property' attribute in the
<iterate> tag,  I get:
--- The error occurred while preparing the mapped statement for execution.
--- Check the myObject.dynamicQuery.
--- Check the parameter map.
--- Cause: com.ibatis.common.beans.ProbeException: Error getting ordinal
list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException:
String index out of range: -1

Thanks for any help!

No virus found in this incoming message.
Checked by AVG.
Version: 8.0.136 / Virus Database: 270.4.6/1540 - Release Date: 7/8/2008
6:33 AM


RE: Problem iterating over a list of beans

by Todd Wilkinson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi-
Thanks for the tip.  I tried that and it didn't work.
The only way I was able to get this to work was to wrap it in a class that
had a property that was a list.  Seems kind of kludgey - but it works
nonetheless.

> I'm certainly not an expert at this, but what if instead of passing the
> list
> right into the query, you created a map and put the list into the map with
> the key "conditions" (then pass the map into the query).
>
> Then, put something like this in the mapping:
>
> <iterate property="conditions" conjunction="and">
> $conditions[].condition$ $conditions[].operator$ #[].value#
> </iterate>
>
> That is usually how I iterate over stuff.  Not sure if I am understanding
> entirely what is going on with your situation, though...
>
> Chris
>
> -----Original Message-----
> From: Todd Wilkinson [mailto:lp@...]
> Sent: Tuesday, July 08, 2008 3:53 PM
> To: user-java@...
> Subject: Problem iterating over a list of beans
>
>
> Hello-
> I'm new to iBATIS and I'm having trouble with the <iterate> element.
>
> I want to create a query for an entirely dynamic where clause.  I've
> constructed a Bean called QueryCondition with the following properties:
>
> String condition
> String operator
> String value
>
> (along with public accessors and mutators)
>
> I'm passing an ArrayList into the following mapping:
>
>   <select id="myObject.dynamicQuery" resultMap="myObject_resultmap"
> resultClass="java.util.List">
>     select id, name, data, notes
>     from objectTable
>     <dynamic prepend="where">
> <iterate conjunction="and">
> $[].condition$ $[].operator$ #[].value#
> </iterate>
> </dynamic>
>   </select>
>
> But it's not working.  In the logs, I see the following:
> Preparing Statement:      select id, name, source_code_location, context,
> notes     from objectTable where       my.package.QueryCondition@1f5b4d1
> my.package.QueryCondition@1f5b4d1 ?
>
> and then I get a logged exception:
> 0com.ibatis.common.jdbc.exception.NestedSQLException:
> --- The error occurred while applying a parameter map.
> --- Check the sites.dynamicQuery-InlineParameterMap.
> --- Check the parameter mapping for the '[0].value' property.
> --- Cause: java.lang.NullPointerException
> [full stack trace available if needed]
> The line 'check the parameter mapping...' will occur for whichever one of
> the properties a place in hashmarks, ie - #[].operator# will cause it
> also.
>
> I have verified that there is 1 QueryCondition object in this list which
> has its properties set.  Strange that the prepared statement resolves the
> first two properties to objects of type QueryCondition, instead of
> property string valyes.  I guess I'm not understanding how to properly
> access the objects in the list.  If I set a 'property' attribute in the
> <iterate> tag,  I get:
> --- The error occurred while preparing the mapped statement for execution.
> --- Check the myObject.dynamicQuery.
> --- Check the parameter map.
> --- Cause: com.ibatis.common.beans.ProbeException: Error getting ordinal
> list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException:
> String index out of range: -1
>
> Thanks for any help!
>
> No virus found in this incoming message.
> Checked by AVG.
> Version: 8.0.136 / Virus Database: 270.4.6/1540 - Release Date: 7/8/2008
> 6:33 AM
>
>



RE: Problem iterating over a list of beans

by Poitras Christian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Putting the List in a Map is a good wayt to be able to specify a property.

If you want to use the list directly, I'd try specifying the property in iterate.

  <select id="myObject.dynamicQuery" resultMap="myObject_resultmap" resultClass="java.util.List">
    select id, name, data, notes
    from objectTable
    <dynamic prepend="where">
              <iterate property="" conjunction="and">
                      $[].condition$ $[].operator$ #[].value#
              </iterate>
      </dynamic>
  </select>

Christian


-----Original Message-----
From: Todd Wilkinson [mailto:lp@...]
Sent: Wednesday, July 09, 2008 9:56 AM
To: user-java@...
Subject: RE: Problem iterating over a list of beans

Hi-
Thanks for the tip.  I tried that and it didn't work.
The only way I was able to get this to work was to wrap it in a class that had a property that was a list.  Seems kind of kludgey - but it works nonetheless.

> I'm certainly not an expert at this, but what if instead of passing
> the list right into the query, you created a map and put the list into
> the map with the key "conditions" (then pass the map into the query).
>
> Then, put something like this in the mapping:
>
> <iterate property="conditions" conjunction="and">
>       $conditions[].condition$ $conditions[].operator$ #[].value#
> </iterate>
>
> That is usually how I iterate over stuff.  Not sure if I am
> understanding entirely what is going on with your situation, though...
>
> Chris
>
> -----Original Message-----
> From: Todd Wilkinson [mailto:lp@...]
> Sent: Tuesday, July 08, 2008 3:53 PM
> To: user-java@...
> Subject: Problem iterating over a list of beans
>
>
> Hello-
> I'm new to iBATIS and I'm having trouble with the <iterate> element.
>
> I want to create a query for an entirely dynamic where clause.  I've
> constructed a Bean called QueryCondition with the following properties:
>
> String condition
> String operator
> String value
>
> (along with public accessors and mutators)
>
> I'm passing an ArrayList into the following mapping:
>
>   <select id="myObject.dynamicQuery" resultMap="myObject_resultmap"
> resultClass="java.util.List">
>     select id, name, data, notes
>     from objectTable
>     <dynamic prepend="where">
>               <iterate conjunction="and">
>                       $[].condition$ $[].operator$ #[].value#
>               </iterate>
>       </dynamic>
>   </select>
>
> But it's not working.  In the logs, I see the following:
> Preparing Statement:      select id, name, source_code_location, context,
> notes     from objectTable where       my.package.QueryCondition@1f5b4d1
> my.package.QueryCondition@1f5b4d1 ?
>
> and then I get a logged exception:
> 0com.ibatis.common.jdbc.exception.NestedSQLException:
> --- The error occurred while applying a parameter map.
> --- Check the sites.dynamicQuery-InlineParameterMap.
> --- Check the parameter mapping for the '[0].value' property.
> --- Cause: java.lang.NullPointerException [full stack trace available
> if needed] The line 'check the parameter mapping...' will occur for
> whichever one of the properties a place in hashmarks, ie -
> #[].operator# will cause it also.
>
> I have verified that there is 1 QueryCondition object in this list
> which has its properties set.  Strange that the prepared statement
> resolves the first two properties to objects of type QueryCondition,
> instead of property string valyes.  I guess I'm not understanding how
> to properly access the objects in the list.  If I set a 'property'
> attribute in the <iterate> tag,  I get:
> --- The error occurred while preparing the mapped statement for execution.
> --- Check the myObject.dynamicQuery.
> --- Check the parameter map.
> --- Cause: com.ibatis.common.beans.ProbeException: Error getting
> ordinal list from JavaBean. Cause java.lang.StringIndexOutOfBoundsException:
> String index out of range: -1
>
> Thanks for any help!
>
> No virus found in this incoming message.
> Checked by AVG.
> Version: 8.0.136 / Virus Database: 270.4.6/1540 - Release Date:
> 7/8/2008
> 6:33 AM
>
>