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 Utkarsh Ahuja <ah...@gmail.com> on 2007/03/27 21:34:06 UTC
Problem creating nested HashMaps using
Hi,
I am trying to create nested HashMaps something like
ParentHashMap {
...... some key value pairs
......
childHashMap....some key value pairs
}
I am doing the following:
<resultMap id="parentResultMap" class="java.util.HashMap">
<result column="parent_column_1" property="parentColumn1" />
<result column="parent_column_1" property="parentColumn2" />
<result column="parent_column_1" property="parentColumn3" />
<!-- customizations -->
<result column="parent_column_1" property="children"
resultMap="childResultMap" select="loadChildren"/>
</resultMap>
<select id="loadParent" parameterClass="java.util.HashMap"
resultMap="parentResultMap">
select
parent_column_1,
parent_column_2,
parent_column_3
from parent
</select>
<resultMap id="childResultMap" class="java.util.HashMap">
<result column="child_column_1" property="childColumn1" />
<result column="child_column_2" property="childColumn2" />
<result column="child_column_3" property="childColumn3" />
<result column="parent_column_1" property="parentColumn1" />
</resultMap>
<select id="loadChildren" resultMap="childResultMap">
select
child_column_1,
child_column_2,
child_column_3,
parent_column_1
from child
where parent_column_1 = #parentColumn1:VARCHAR#
</select>
Can iBatis instantiate and create a HashMap within another HashMap and use
the property element as the key for it ?
<result column="parent_column_1" property="children"
resultMap="childResultMap" select="loadChildren"/>
Thanks
Utkarsh
Re: Problem creating nested HashMaps using
Posted by Larry Meadors <lm...@apache.org>.
LOL, I like that.. "trashtable", hahaha. :-D
Larry
On 3/28/07, Ted Schrader <te...@gmail.com> wrote:
> Ah, so the hashtables are simply used as buckets in a very limited,
> direct scope, i.e. iBATIS --> JasperReports. That's not so bad;
> similar to using Maps as parameter objects to iBATIS. I wouldn't
> consider this to be a domain model.
>
> I wish my situation was different: instead of an OrderVO object, I
> have to deal with Hashtable. Instead of ItemVO, I deal with
> Hashtable. Instead of UserVO, you guessed it...Hashtable. Ugh.
> "Trashtable", indeed.
>
> Good job figuring this out and avoiding the N+1 select, to boot.
>
> Ted
>
> On 28/03/07, Utkarsh Ahuja <ah...@gmail.com> wrote:
> > Dear Ted,
> >
> > The hashtables in hashtables sounds like a really really bad idea
> > ....:)....I can totally see that :). The Pojo's do seem to be the best way
> > to go and for the most part thats what I have been doing (The tried and
> > tested ofcourse). I am doing this as we want to create an independent
> > application that hits the same relational database however, we want to be
> > able to extract whatever we want from it without being tied down to the
> > POJO's(JasperReports) . Simply to eliminate the need for maintenance.
> >
> > I did try to use XML earlier using ibatis but that did not work out too well
> > and it was really slow when I provided an xml document to JasperReports as a
> > datasource.
> >
> > However, I did figure something out late last night. The way to do it is
> > something like this. Using groupBy and the resultMap and using a join in the
> > select statement
> >
> > <resultMap id="parentResultMap" class="java.util.HashMap"
> > groupBy="parentProperty1,parentProperty2,parentProperty3,parentProperty4,parentProperty5"
> > >
> > <result column="parent_property_1" property="parentProperty1" />
> > <result column="parent_property_2" property="parentProperty2" />
> > <result column="parent_property_3" property="parentProperty3" />
> > <result column="parent_property_4" property="parentProperty4" />
> > <result column="parent_property_5" property="parentProperty5" />
> > <result property="children" javaType="java.util.List"
> > resultMap="namespace.childResultMap"/>
> > </resultMap>
> >
> > <resultMap id="childResultMap" class=" java.util.HashMap">
> > <result column="child_property_1" property="childProperty1" />
> > <result column="child_property_2" property="childProperty2" />
> > <result column="child_property_3" property="childProperty3" />
> > <result column="parent_property_1" property="parentProperty1" />
> > </resultMap>
> >
> > <select id="parentChildMapReport" parameterClass="java.util.HashMap"
> > resultMap="parentResultMap">
> > select
> > p.parent_property_1,
> > p.parent_property_2 ,
> > p.parent_property_3,
> > p.parent_property_4,
> > p.parent_property_5,
> > c.child_property_1,
> > c.child_property_2,
> > c.child_property_3 ,
> > c.parent_property_1
> > from parent p, child c
> > where p.parent_property_1 = c.parent_property_1
> > </select>
> >
> > This worked and did solve our problem where we could just have a generic
> > object to hold the results and we were able to pass it as a datasource to
> > jasper reports. Performance wise hashtables work really really well(with
> > JasperReports).
> >
> > Thanks
> > Utkarsh
> >
> >
> >
> > On 3/28/07, Ted Schrader <te...@gmail.com> wrote:
> > > I'm not sure about the ultimate answer, but I'm also not sure the
> > > following is valid:
> > >
> > > <result column="parent_column_1" property="children"
> > > resultMap="childResultMap" select="loadChildren"/>
> > >
> > > I believe the "resultMap" and "select" attributes are mutually
> > > exclusive ("resultMap" for avoiding N+1 selects, and "select" for
> > > sub-selects, which in turn have their own resultMaps).
> > >
> > > Also, at risk of hijacking the thread, I just have to throw in my two
> > cents:
> > >
> > > Call me a coward, but hashtables inside hashtables as a domain model
> > > frightens the living daylights out of me. We've just inherited a web
> > > app that uses this "design pattern" and we quickly bestowed the
> > > honorable title of "trashtable" to the idiom. Perhaps I'm just
> > > entrenched in my ways, but I prefer POJOs and the ability to really
> > > lean on the compiler (thus my biggest gripe about JSP/Struts tags). I
> > > would love to hear about any compelling arguments for the hash-in-hash
> > > model in an off-topic thread.
> > >
> > > Ted
> > >
> > > On 27/03/07, Utkarsh Ahuja <ahuja.utkarsh@gmail.com > wrote:
> > > > Hi,
> > > > I am trying to create nested HashMaps something like
> > > >
> > > > ParentHashMap {
> > > > ...... some key value pairs
> > > > ......
> > > > childHashMap....some key value pairs
> > > > }
> > > >
> > > > I am doing the following:
> > > >
> > > > <resultMap id="parentResultMap" class="java.util.HashMap">
> > > > <result column="parent_column_1" property="parentColumn1" />
> > > > <result column="parent_column_1" property="parentColumn2" />
> > > > <result column="parent_column_1" property="parentColumn3" />
> > > > <!-- customizations -->
> > > > <result column="parent_column_1" property="children"
> > > > resultMap="childResultMap" select="loadChildren"/>
> > > > </resultMap>
> > > >
> > > > <select id="loadParent" parameterClass=" java.util.HashMap"
> > > > resultMap="parentResultMap">
> > > > select
> > > > parent_column_1,
> > > > parent_column_2,
> > > > parent_column_3
> > > > from parent
> > > > </select>
> > > >
> > > > <resultMap id="childResultMap" class="java.util.HashMap">
> > > > <result column="child_column_1" property="childColumn1" />
> > > > <result column="child_column_2" property="childColumn2" />
> > > > <result column="child_column_3" property="childColumn3" />
> > > > <result column="parent_column_1" property="parentColumn1" />
> > > > </resultMap>
> > > >
> > > > <select id="loadChildren" resultMap="childResultMap">
> > > > select
> > > > child_column_1,
> > > > child_column_2,
> > > > child_column_3,
> > > > parent_column_1
> > > > from child
> > > > where parent_column_1 = #parentColumn1:VARCHAR#
> > > > </select>
> > > >
> > > > Can iBatis instantiate and create a HashMap within another HashMap and
> > use
> > > > the property element as the key for it ?
> > > > <result column="parent_column_1" property="children"
> > > > resultMap="childResultMap" select="loadChildren"/>
> > > >
> > > > Thanks
> > > > Utkarsh
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> >
> >
>
Re: Problem creating nested HashMaps using
Posted by Ted Schrader <te...@gmail.com>.
Ah, so the hashtables are simply used as buckets in a very limited,
direct scope, i.e. iBATIS --> JasperReports. That's not so bad;
similar to using Maps as parameter objects to iBATIS. I wouldn't
consider this to be a domain model.
I wish my situation was different: instead of an OrderVO object, I
have to deal with Hashtable. Instead of ItemVO, I deal with
Hashtable. Instead of UserVO, you guessed it...Hashtable. Ugh.
"Trashtable", indeed.
Good job figuring this out and avoiding the N+1 select, to boot.
Ted
On 28/03/07, Utkarsh Ahuja <ah...@gmail.com> wrote:
> Dear Ted,
>
> The hashtables in hashtables sounds like a really really bad idea
> ....:)....I can totally see that :). The Pojo's do seem to be the best way
> to go and for the most part thats what I have been doing (The tried and
> tested ofcourse). I am doing this as we want to create an independent
> application that hits the same relational database however, we want to be
> able to extract whatever we want from it without being tied down to the
> POJO's(JasperReports) . Simply to eliminate the need for maintenance.
>
> I did try to use XML earlier using ibatis but that did not work out too well
> and it was really slow when I provided an xml document to JasperReports as a
> datasource.
>
> However, I did figure something out late last night. The way to do it is
> something like this. Using groupBy and the resultMap and using a join in the
> select statement
>
> <resultMap id="parentResultMap" class="java.util.HashMap"
> groupBy="parentProperty1,parentProperty2,parentProperty3,parentProperty4,parentProperty5"
> >
> <result column="parent_property_1" property="parentProperty1" />
> <result column="parent_property_2" property="parentProperty2" />
> <result column="parent_property_3" property="parentProperty3" />
> <result column="parent_property_4" property="parentProperty4" />
> <result column="parent_property_5" property="parentProperty5" />
> <result property="children" javaType="java.util.List"
> resultMap="namespace.childResultMap"/>
> </resultMap>
>
> <resultMap id="childResultMap" class=" java.util.HashMap">
> <result column="child_property_1" property="childProperty1" />
> <result column="child_property_2" property="childProperty2" />
> <result column="child_property_3" property="childProperty3" />
> <result column="parent_property_1" property="parentProperty1" />
> </resultMap>
>
> <select id="parentChildMapReport" parameterClass="java.util.HashMap"
> resultMap="parentResultMap">
> select
> p.parent_property_1,
> p.parent_property_2 ,
> p.parent_property_3,
> p.parent_property_4,
> p.parent_property_5,
> c.child_property_1,
> c.child_property_2,
> c.child_property_3 ,
> c.parent_property_1
> from parent p, child c
> where p.parent_property_1 = c.parent_property_1
> </select>
>
> This worked and did solve our problem where we could just have a generic
> object to hold the results and we were able to pass it as a datasource to
> jasper reports. Performance wise hashtables work really really well(with
> JasperReports).
>
> Thanks
> Utkarsh
>
>
>
> On 3/28/07, Ted Schrader <te...@gmail.com> wrote:
> > I'm not sure about the ultimate answer, but I'm also not sure the
> > following is valid:
> >
> > <result column="parent_column_1" property="children"
> > resultMap="childResultMap" select="loadChildren"/>
> >
> > I believe the "resultMap" and "select" attributes are mutually
> > exclusive ("resultMap" for avoiding N+1 selects, and "select" for
> > sub-selects, which in turn have their own resultMaps).
> >
> > Also, at risk of hijacking the thread, I just have to throw in my two
> cents:
> >
> > Call me a coward, but hashtables inside hashtables as a domain model
> > frightens the living daylights out of me. We've just inherited a web
> > app that uses this "design pattern" and we quickly bestowed the
> > honorable title of "trashtable" to the idiom. Perhaps I'm just
> > entrenched in my ways, but I prefer POJOs and the ability to really
> > lean on the compiler (thus my biggest gripe about JSP/Struts tags). I
> > would love to hear about any compelling arguments for the hash-in-hash
> > model in an off-topic thread.
> >
> > Ted
> >
> > On 27/03/07, Utkarsh Ahuja <ahuja.utkarsh@gmail.com > wrote:
> > > Hi,
> > > I am trying to create nested HashMaps something like
> > >
> > > ParentHashMap {
> > > ...... some key value pairs
> > > ......
> > > childHashMap....some key value pairs
> > > }
> > >
> > > I am doing the following:
> > >
> > > <resultMap id="parentResultMap" class="java.util.HashMap">
> > > <result column="parent_column_1" property="parentColumn1" />
> > > <result column="parent_column_1" property="parentColumn2" />
> > > <result column="parent_column_1" property="parentColumn3" />
> > > <!-- customizations -->
> > > <result column="parent_column_1" property="children"
> > > resultMap="childResultMap" select="loadChildren"/>
> > > </resultMap>
> > >
> > > <select id="loadParent" parameterClass=" java.util.HashMap"
> > > resultMap="parentResultMap">
> > > select
> > > parent_column_1,
> > > parent_column_2,
> > > parent_column_3
> > > from parent
> > > </select>
> > >
> > > <resultMap id="childResultMap" class="java.util.HashMap">
> > > <result column="child_column_1" property="childColumn1" />
> > > <result column="child_column_2" property="childColumn2" />
> > > <result column="child_column_3" property="childColumn3" />
> > > <result column="parent_column_1" property="parentColumn1" />
> > > </resultMap>
> > >
> > > <select id="loadChildren" resultMap="childResultMap">
> > > select
> > > child_column_1,
> > > child_column_2,
> > > child_column_3,
> > > parent_column_1
> > > from child
> > > where parent_column_1 = #parentColumn1:VARCHAR#
> > > </select>
> > >
> > > Can iBatis instantiate and create a HashMap within another HashMap and
> use
> > > the property element as the key for it ?
> > > <result column="parent_column_1" property="children"
> > > resultMap="childResultMap" select="loadChildren"/>
> > >
> > > Thanks
> > > Utkarsh
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> >
>
>
Re: Problem creating nested HashMaps using
Posted by Utkarsh Ahuja <ah...@gmail.com>.
Dear Ted,
The hashtables in hashtables sounds like a really really bad idea
....:)....I can totally see that :). The Pojo's do seem to be the best way
to go and for the most part thats what I have been doing (The tried and
tested ofcourse). I am doing this as we want to create an independent
application that hits the same relational database however, we want to be
able to extract whatever we want from it without being tied down to the
POJO's(JasperReports) . Simply to eliminate the need for maintenance.
I did try to use XML earlier using ibatis but that did not work out too well
and it was really slow when I provided an xml document to JasperReports as a
datasource.
However, I did figure something out late last night. The way to do it is
something like this. Using groupBy and the resultMap and using a join in the
select statement
<resultMap id="parentResultMap" class="java.util.HashMap"
groupBy="parentProperty1,parentProperty2,parentProperty3,parentProperty4,parentProperty5"
>
<result column="parent_property_1" property="parentProperty1" />
<result column="parent_property_2" property="parentProperty2" />
<result column="parent_property_3" property="parentProperty3" />
<result column="parent_property_4" property="parentProperty4" />
<result column="parent_property_5" property="parentProperty5" />
<result property="children" javaType="java.util.List" resultMap="
namespace.childResultMap"/>
</resultMap>
<resultMap id="childResultMap" class="java.util.HashMap">
<result column="child_property_1" property="childProperty1" />
<result column="child_property_2" property="childProperty2" />
<result column="child_property_3" property="childProperty3" />
<result column="parent_property_1" property="parentProperty1" />
</resultMap>
<select id="parentChildMapReport" parameterClass="java.util.HashMap"
resultMap="parentResultMap">
select
p.parent_property_1,
p.parent_property_2,
p.parent_property_3,
p.parent_property_4,
p.parent_property_5,
c.child_property_1,
c.child_property_2,
c.child_property_3,
c.parent_property_1
from parent p, child c
where p.parent_property_1 = c.parent_property_1
</select>
This worked and did solve our problem where we could just have a generic
object to hold the results and we were able to pass it as a datasource to
jasper reports. Performance wise hashtables work really really well(with
JasperReports).
Thanks
Utkarsh
On 3/28/07, Ted Schrader <te...@gmail.com> wrote:
>
> I'm not sure about the ultimate answer, but I'm also not sure the
> following is valid:
>
> <result column="parent_column_1" property="children"
> resultMap="childResultMap" select="loadChildren"/>
>
> I believe the "resultMap" and "select" attributes are mutually
> exclusive ("resultMap" for avoiding N+1 selects, and "select" for
> sub-selects, which in turn have their own resultMaps).
>
> Also, at risk of hijacking the thread, I just have to throw in my two
> cents:
>
> Call me a coward, but hashtables inside hashtables as a domain model
> frightens the living daylights out of me. We've just inherited a web
> app that uses this "design pattern" and we quickly bestowed the
> honorable title of "trashtable" to the idiom. Perhaps I'm just
> entrenched in my ways, but I prefer POJOs and the ability to really
> lean on the compiler (thus my biggest gripe about JSP/Struts tags). I
> would love to hear about any compelling arguments for the hash-in-hash
> model in an off-topic thread.
>
> Ted
>
> On 27/03/07, Utkarsh Ahuja <ah...@gmail.com> wrote:
> > Hi,
> > I am trying to create nested HashMaps something like
> >
> > ParentHashMap {
> > ...... some key value pairs
> > ......
> > childHashMap....some key value pairs
> > }
> >
> > I am doing the following:
> >
> > <resultMap id="parentResultMap" class="java.util.HashMap">
> > <result column="parent_column_1" property="parentColumn1" />
> > <result column="parent_column_1" property="parentColumn2" />
> > <result column="parent_column_1" property="parentColumn3" />
> > <!-- customizations -->
> > <result column="parent_column_1" property="children"
> > resultMap="childResultMap" select="loadChildren"/>
> > </resultMap>
> >
> > <select id="loadParent" parameterClass="java.util.HashMap"
> > resultMap="parentResultMap">
> > select
> > parent_column_1,
> > parent_column_2,
> > parent_column_3
> > from parent
> > </select>
> >
> > <resultMap id="childResultMap" class="java.util.HashMap">
> > <result column="child_column_1" property="childColumn1" />
> > <result column="child_column_2" property="childColumn2" />
> > <result column="child_column_3" property="childColumn3" />
> > <result column="parent_column_1" property="parentColumn1" />
> > </resultMap>
> >
> > <select id="loadChildren" resultMap="childResultMap">
> > select
> > child_column_1,
> > child_column_2,
> > child_column_3,
> > parent_column_1
> > from child
> > where parent_column_1 = #parentColumn1:VARCHAR#
> > </select>
> >
> > Can iBatis instantiate and create a HashMap within another HashMap and
> use
> > the property element as the key for it ?
> > <result column="parent_column_1" property="children"
> > resultMap="childResultMap" select="loadChildren"/>
> >
> > Thanks
> > Utkarsh
> >
> >
> >
> >
> >
> >
> >
> >
>
Re: Problem creating nested HashMaps using
Posted by Ted Schrader <te...@gmail.com>.
I'm not sure about the ultimate answer, but I'm also not sure the
following is valid:
<result column="parent_column_1" property="children"
resultMap="childResultMap" select="loadChildren"/>
I believe the "resultMap" and "select" attributes are mutually
exclusive ("resultMap" for avoiding N+1 selects, and "select" for
sub-selects, which in turn have their own resultMaps).
Also, at risk of hijacking the thread, I just have to throw in my two cents:
Call me a coward, but hashtables inside hashtables as a domain model
frightens the living daylights out of me. We've just inherited a web
app that uses this "design pattern" and we quickly bestowed the
honorable title of "trashtable" to the idiom. Perhaps I'm just
entrenched in my ways, but I prefer POJOs and the ability to really
lean on the compiler (thus my biggest gripe about JSP/Struts tags). I
would love to hear about any compelling arguments for the hash-in-hash
model in an off-topic thread.
Ted
On 27/03/07, Utkarsh Ahuja <ah...@gmail.com> wrote:
> Hi,
> I am trying to create nested HashMaps something like
>
> ParentHashMap {
> ...... some key value pairs
> ......
> childHashMap....some key value pairs
> }
>
> I am doing the following:
>
> <resultMap id="parentResultMap" class="java.util.HashMap">
> <result column="parent_column_1" property="parentColumn1" />
> <result column="parent_column_1" property="parentColumn2" />
> <result column="parent_column_1" property="parentColumn3" />
> <!-- customizations -->
> <result column="parent_column_1" property="children"
> resultMap="childResultMap" select="loadChildren"/>
> </resultMap>
>
> <select id="loadParent" parameterClass="java.util.HashMap"
> resultMap="parentResultMap">
> select
> parent_column_1,
> parent_column_2,
> parent_column_3
> from parent
> </select>
>
> <resultMap id="childResultMap" class="java.util.HashMap">
> <result column="child_column_1" property="childColumn1" />
> <result column="child_column_2" property="childColumn2" />
> <result column="child_column_3" property="childColumn3" />
> <result column="parent_column_1" property="parentColumn1" />
> </resultMap>
>
> <select id="loadChildren" resultMap="childResultMap">
> select
> child_column_1,
> child_column_2,
> child_column_3,
> parent_column_1
> from child
> where parent_column_1 = #parentColumn1:VARCHAR#
> </select>
>
> Can iBatis instantiate and create a HashMap within another HashMap and use
> the property element as the key for it ?
> <result column="parent_column_1" property="children"
> resultMap="childResultMap" select="loadChildren"/>
>
> Thanks
> Utkarsh
>
>
>
>
>
>
>
>