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 Rick Accountius <ri...@us.ibm.com> on 2007/07/02 23:27:31 UTC

Help with HashMap

Greetings, 

I'm kind of new to Ibatis, so if I'm missing the obvious, let me apologize 


right up front. 

Nonetheless, here is my problem.  I am using a HashMap as the resultClass 
to retrieve results from a DB2 database.  My problem is the column order 
(not row order) returned appears to be almost random.  How do I "force" 
Ibatis to return the columns in the same order as they appear (returned) 
in the SQL query? 

For example, here is my query:

SELECT A as COL_A, B as COL_B, C as COL_C from tablexxxx

My results can be COL_B, COL_A, COL_C..........or COL_C, COL_B, 
COL_A..........or , etc, etc.

I have logged java.sql and the columns are being returned in the proper 
order, but eventually they get "realigned".  Can someone explain to me 
why?  I'd really appreciate it.  I know I can use a ResultMap to have the 
columns listed in the proper order, but what I'm trying to do is use the 
same Ibatis select statement to "service" arbitrary SQL statements.  Hope 
I'm making sense.

<select id="getReport" parameterClass="com.ibm.bean.ReportDefinition" 
resultClass="java.util.HashMap" remapResults="true">
        $query$
</select>

Thanks, 

Regards, 

Rick Accountius 
Internet: rickac@us.ibm.com

Re: Help with HashMap

Posted by Larry Meadors <lm...@apache.org>.
I'm guessing by the names that beans are not going to work for this -
the field list is probably pretty fluid.

LinkedHashMap might work, try that first, but if not, then if you have
the field list from the query that you are building, you could use it
with a c:forEach or StringTokenizer to get the field names in order.

Larry


On 7/2/07, Nathan Maves <na...@gmail.com> wrote:
> Apology accepted.
>
> First off lets take a trip down the old API.
>
> from
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html
>
> "This class makes no guarantees as to the order of the map; in particular,
> it does not guarantee that the order will remain constant over time."
>
> Second, you can create the order when you pull them out.  But I am guessing
> that you are using some sort of iterator to loop through the map and print
> out the results, since you dont know the column names.  I can only suggest
> that you try to create some result classes and not use maps.  Maps are great
> for parameter maps but are generally frowned upon as a result class.  As a
> last resort you might get away with trying to use LlinkedHashMap from
> java.util.
>
> Nathan
>
>
>
> On 7/2/07, Rick Accountius <rickac@us.ibm.com > wrote:
> >
> > Greetings,
> >
> > I'm kind of new to Ibatis, so if I'm missing the obvious, let me apologize
> >
> > right up front.
> >
> > Nonetheless, here is my problem.  I am using a HashMap as the resultClass
> > to retrieve results from a DB2 database.  My problem is the column order
> > (not row order) returned appears to be almost random.  How do I "force"
> > Ibatis to return the columns in the same order as they appear (returned)
> > in the SQL query?
> >
> > For example, here is my query:
> >
> > SELECT A as COL_A, B as COL_B, C as COL_C from tablexxxx
> >
> > My results can be COL_B, COL_A, COL_C..........or COL_C, COL_B,
> > COL_A..........or , etc, etc.
> >
> > I have logged java.sql and the columns are being returned in the proper
> > order, but eventually they get "realigned".  Can someone explain to me
> > why?  I'd really appreciate it.  I know I can use a ResultMap to have the
> > columns listed in the proper order, but what I'm trying to do is use the
> > same Ibatis select statement to "service" arbitrary SQL statements.  Hope
> > I'm making sense.
> >
> > <select id="getReport" parameterClass="com.ibm.bean.ReportDefinition"
> > resultClass="java.util.HashMap" remapResults="true">
> >        $query$
> > </select>
> >
> > Thanks,
> >
> > Regards,
> >
> > Rick Accountius
> > Internet: rickac@us.ibm.com
>
>

Re: Help with HashMap

Posted by Rick Accountius <ri...@us.ibm.com>.
Thanks Nathan!  Not the answer I was hoping for, but it does make sense. 
Guess I should have looked at the Java API to begin with.  DOH!  Thanks 
again.

Rick Accountius 
Internet: rickac@us.ibm.com




"Nathan Maves" <na...@gmail.com> 
07/02/07 03:48 PM
Please respond to
user-java@ibatis.apache.org


To
user-java@ibatis.apache.org
cc

Subject
Re: Help with HashMap






Apology accepted.

First off lets take a trip down the old API.

from http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html

"This class makes no guarantees as to the order of the map; in particular, 
it does not guarantee that the order will remain constant over time."

Second, you can create the order when you pull them out.  But I am 
guessing that you are using some sort of iterator to loop through the map 
and print out the results, since you dont know the column names.  I can 
only suggest that you try to create some result classes and not use maps. 
Maps are great for parameter maps but are generally frowned upon as a 
result class.  As a last resort you might get away with trying to use 
LlinkedHashMap from java.util.

Nathan


On 7/2/07, Rick Accountius <rickac@us.ibm.com > wrote:

Greetings, 

I'm kind of new to Ibatis, so if I'm missing the obvious, let me apologize 


right up front. 

Nonetheless, here is my problem.  I am using a HashMap as the resultClass 
to retrieve results from a DB2 database.  My problem is the column order 
(not row order) returned appears to be almost random.  How do I "force" 
Ibatis to return the columns in the same order as they appear (returned) 
in the SQL query? 

For example, here is my query:

SELECT A as COL_A, B as COL_B, C as COL_C from tablexxxx

My results can be COL_B, COL_A, COL_C..........or COL_C, COL_B, 
COL_A..........or , etc, etc.

I have logged java.sql and the columns are being returned in the proper 
order, but eventually they get "realigned".  Can someone explain to me 
why?  I'd really appreciate it.  I know I can use a ResultMap to have the 
columns listed in the proper order, but what I'm trying to do is use the 
same Ibatis select statement to "service" arbitrary SQL statements.  Hope 
I'm making sense.

<select id="getReport" parameterClass="com.ibm.bean.ReportDefinition" 
resultClass="java.util.HashMap" remapResults="true">
       $query$
</select>

Thanks, 

Regards, 

Rick Accountius 
Internet: rickac@us.ibm.com


Re: Help with HashMap

Posted by Nathan Maves <na...@gmail.com>.
Apology accepted.

First off lets take a trip down the old API.

from http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html

"This class makes no guarantees as to the order of the map; in particular,
it does not guarantee that the order will remain constant over time."

Second, you can create the order when you pull them out.  But I am guessing
that you are using some sort of iterator to loop through the map and print
out the results, since you dont know the column names.  I can only suggest
that you try to create some result classes and not use maps.  Maps are great
for parameter maps but are generally frowned upon as a result class.  As a
last resort you might get away with trying to use LlinkedHashMap from
java.util.

Nathan


On 7/2/07, Rick Accountius <ri...@us.ibm.com> wrote:
>
>
> Greetings,
>
> I'm kind of new to Ibatis, so if I'm missing the obvious, let me apologize
>
>
> right up front.
>
> Nonetheless, here is my problem.  I am using a HashMap as the resultClass
> to retrieve results from a DB2 database.  My problem is the column order
> (not row order) returned appears to be almost random.  How do I "force"
> Ibatis to return the columns in the same order as they appear (returned)
> in the SQL query?
>
> For example, here is my query:
>
> SELECT A as COL_A, B as COL_B, C as COL_C from tablexxxx
>
> My results can be COL_B, COL_A, COL_C..........or COL_C, COL_B,
> COL_A..........or , etc, etc.
>
> I have logged java.sql and the columns are being returned in the proper
> order, but eventually they get "realigned".  Can someone explain to me
> why?  I'd really appreciate it.  I know I can use a ResultMap to have the
> columns listed in the proper order, but what I'm trying to do is use the
> same Ibatis select statement to "service" arbitrary SQL statements.  Hope
> I'm making sense.
>
> <select id="getReport" parameterClass="com.ibm.bean.ReportDefinition"
> resultClass="java.util.HashMap" remapResults="true">
>        $query$
> </select>
>
> Thanks,
>
> Regards,
>
> Rick Accountius
> Internet: rickac@us.ibm.com