Message from discussion
Is there a way to nest result maps or turn off the roll-up behavior of associations and collections?
Received: by 10.58.216.36 with SMTP id on4mr6604456vec.28.1351803751899;
Thu, 01 Nov 2012 14:02:31 -0700 (PDT)
X-BeenThere: mybatis-user@googlegroups.com
Received: by 10.220.108.198 with SMTP id g6ls2925707vcp.1.gmail; Thu, 01 Nov
2012 14:02:30 -0700 (PDT)
Received: by 10.52.76.136 with SMTP id k8mr5949442vdw.13.1351803750259;
Thu, 01 Nov 2012 14:02:30 -0700 (PDT)
Date: Thu, 1 Nov 2012 14:02:29 -0700 (PDT)
From: dq <d...@kayak.com>
To: mybatis-user@googlegroups.com
Message-Id: <b417ecee-cd5e-4eb7-b774-847e3bee841f@googlegroups.com>
In-Reply-To: <CAKpaNj6y4wuBYC2Wo6WsPhqR3M6swHUbp+QvtOEKW3+e8YoL6Q@mail.gmail.com>
References: <2f3d6299-86a8-4fa6-be7a-0ede06049b30@googlegroups.com>
<17eae825-8539-4a2d-a7c9-229e5fca9d02@googlegroups.com>
<CAKpaNj6jTWKbmC3UD9W=78VjrTOVdHatTvHpaEncqPOC_8AbsA@mail.gmail.com>
<91b179fb-57d3-46fd-8121-040d766f54a4@googlegroups.com>
<CAKpaNj5zf0--SWvsO7U8VUUxFghqP=Uttc2nPo-GZXmrH_YbVg@mail.gmail.com>
<CAKpaNj6y4wuBYC2Wo6WsPhqR3M6swHUbp+QvtOEKW3+e8YoL6Q@mail.gmail.com>
Subject: Re: Is there a way to nest result maps or turn off the roll-up
behavior of associations and collections?
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_841_16794100.1351803749452"
------=_Part_841_16794100.1351803749452
Content-Type: multipart/alternative;
boundary="----=_Part_842_32113905.1351803749452"
------=_Part_842_32113905.1351803749452
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
The car example was contrived, but I used it to explain the problem because
the abcd example seemed to abstract to describe without confusing people.
The abcd example is very close to the situation I've got. We have a
situation where we have rows in a table with all columns comprising the
primary key. The first column is an ID that's a reference to another table
and the data from all the other columns is put into a different object type.
So an Abcd has an ID and a Bcd, and another object contains a List of
Abcds.
I don't see anything wrong with that design. I guess you could argue that
we should add a primary key column and then have a unique key on the
combination of columns a,b,c,d. That's a valid critique, but I'm not sure
it's valid in relation to this problem I'm having because there could be a
situation where you want to perform a select on a table (or multiple
tables) and only select certain columns that result in duplicate results
being returned.
On Thursday, November 1, 2012 4:52:02 PM UTC-4, Jeff Butler wrote:
>
> I got kind of interested in this and coded a test. The problem with your
> example is that there is nothing to distinguish a unique Abcd (or car). If
> you can add an attribute to your car (carId???) that makes it unique, then
> it will work as you expect.
>
> Your example is so contrived that it is hard to see how this translates to
> a real life problem. In coding my test I had to write some bad SQL to make
> it fail in the way you are reporting. I understand you're moving from 2.x
> to 3.x. This may be a good chance to revisit some of these designs and
> improve them.
>
> If you're interested in looking at the tests, they are here:
>
>
> http://mybatis.googlecode.com/svn/trunk/src/test/java/org/apache/ibatis/submitted/associationtest/
>
> One test works the way you are hoping it would (with unique cars). The
> other test work the way you are reporting (with non-unique cars).
>
> Jeff Butler
>
>
>
>
>
> On Thu, Nov 1, 2012 at 3:53 PM, Jeff Butler <jeffg...@gmail.com<javascript:>
> > wrote:
>
>> I *think* MyBatis will make the Bcd object for you - so you don't need to
>> create it in the Abcd constructor.
>>
>> You are right that this is a copy/paste job if Bcd is in a lot of objects.
>>
>> Jeff Butler
>>
>>
>> On Thu, Nov 1, 2012 at 3:38 PM, dq <d...@kayak.com <javascript:>> wrote:
>>
>>> Interesting. I think I prefer constructors over this... Using
>>> <constructor>s each resultMap is responsible for constructing the object
>>> whereas this way in order for bcd.b setter to work, the default constructor
>>> of Abcd would need to create an empty Bcd, and I don't get the benefit of
>>> re-using resultMaps so every object that has a Bcd would need to include
>>> those 3 <result> lines in its resultMap.
>>>
>>> But good to know that's possible, thanks! It may come in very handy in
>>> a different situation.
>>>
>>>
>>> On Thursday, November 1, 2012 3:16:47 PM UTC-4, Jeff Butler wrote:
>>>
>>>> This probably isn't exactly what you're looking for either, but it
>>>> should work:
>>>>
>>>> <resultMap id="abcd" type="com.Abcd">
>>>> <result property="a" column="a"/>
>>>> <result property="bcd.b" column="b"/>
>>>> <result property="bcd.c" column="c"/>
>>>> <result property="bcd.d" column="d"/>
>>>> </resultMap>
>>>>
>>>> Jeff Butler
>>>>
>>>>
>>>>
>>>> On Thu, Nov 1, 2012 at 2:58 PM, dq <d...@kayak.com> wrote:
>>>>
>>>>> The solution I (don't like) found is to use resultMaps with the
>>>>> <constructor> tag, which thanks to http://code.google.com/p/**
>>>>> mybatis/issues/detail?id=15<http://code.google.com/p/mybatis/issues/detail?id=15>
>>>>> **can call a constructor on the encompassing object passing in the
>>>>> objects constructed in its constituent resultMaps. I'd still prefer being
>>>>> able to do it the iBatis way so I don't have to create constructors to
>>>>> satisfy the DAO.
>>>>
>>>>
>>>>
>>
>
------=_Part_842_32113905.1351803749452
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
The car example was contrived, but I used it to explain the problem because=
the abcd example seemed to abstract to describe without confusing people.<=
div><br></div><div>The abcd example is very close to the situation I've got=
. We have a situation where we have rows in a table with all columns =
comprising the primary key. The first column is an ID that's a refere=
nce to another table and the data from all the other columns is put into a =
different object type.</div><div><br></div><div>So an Abcd has an ID and a =
Bcd, and another object contains a List of Abcds. </div><div><br></div=
><div>I don't see anything wrong with that design. I guess you could =
argue that we should add a primary key column and then have a unique key on=
the combination of columns a,b,c,d. That's a valid critique, but I'm=
not sure it's valid in relation to this problem I'm having because there c=
ould be a situation where you want to perform a select on a table (or multi=
ple tables) and only select certain columns that result in duplicate result=
s being returned. <br><br>On Thursday, November 1, 2012 4:52:02 PM UT=
C-4, Jeff Butler wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0=
;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">I got k=
ind of interested in this and coded a test. The problem with your exa=
mple is that there is nothing to distinguish a unique Abcd (or car). =
If you can add an attribute to your car (carId???) that makes it unique, th=
en it will work as you expect.<div>
<br></div><div>Your example is so contrived that it is hard to see how this=
translates to a real life problem. In coding my test I had to write =
some bad SQL to make it fail in the way you are reporting. I understa=
nd you're moving from 2.x to 3.x. This may be a good chance to revisi=
t some of these designs and improve them.</div>
<div><br></div><div>If you're interested in looking at the tests, they are =
here:</div><div><br></div><div><a href=3D"http://mybatis.googlecode.com/svn=
/trunk/src/test/java/org/apache/ibatis/submitted/associationtest/" target=
=3D"_blank">http://mybatis.googlecode.com/<wbr>svn/trunk/src/test/java/org/=
<wbr>apache/ibatis/submitted/<wbr>associationtest/</a><br>
</div><div><br></div><div>One test works the way you are hoping it would (w=
ith unique cars). The other test work the way you are reporting (with=
non-unique cars).</div><div><br></div><div>Jeff Butler</div><div><br></div=
>
<div><br></div><div><br></div><div><br><br><div class=3D"gmail_quote">On Th=
u, Nov 1, 2012 at 3:53 PM, Jeff Butler <span dir=3D"ltr"><<a href=3D"jav=
ascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"1wiT6cyavo4J">jeffg...=
@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">I *think* MyBatis will make the Bcd object f=
or you - so you don't need to create it in the Abcd constructor.<div><br>
</div><div>You are right that this is a copy/paste job if Bcd is in a lot o=
f objects.</div><span><font color=3D"#888888"><div><br></div>
<div>Jeff Butler</div></font></span><div><div><div><br><br><div class=3D"gm=
ail_quote">On Thu, Nov 1, 2012 at 3:38 PM, dq <span dir=3D"ltr"><<a href=
=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"1wiT6cyavo4J">d=
....@kayak.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Interesting. I think I prefer constructors over this... Using &=
lt;constructor>s each resultMap is responsible for constructing the obje=
ct whereas this way in order for bcd.b setter to work, the default construc=
tor of Abcd would need to create an empty Bcd, and I don't get the benefit =
of re-using resultMaps so every object that has a Bcd would need to include=
those 3 <result> lines in its resultMap.<div>
<br></div><div>But good to know that's possible, thanks! It may come =
in very handy in a different situation.<div><br><br>On Thursday, November 1=
, 2012 3:16:47 PM UTC-4, Jeff Butler wrote:</div><blockquote class=3D"gmail=
_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padd=
ing-left:1ex">
<div><div style=3D"font-family:arial,sans-serif;font-size:13px">This probab=
ly isn't exactly what you're looking for either, but it should work:</div><=
div style=3D"font-family:arial,sans-serif;font-size:13px">
<br></div><div style=3D"font-family:arial,sans-serif;font-size:13px">
<resultMap id=3D"abcd" type=3D"com.Abcd"></div><div style=3D"font-fam=
ily:arial,sans-serif;font-size:13px"> <result property=3D"a" colum=
n=3D"a"/></div><div style=3D"font-family:arial,sans-serif;font-size:13px=
">
<result property=3D"bcd.b" column=3D"b"/></div><div style=3D"f=
ont-size:13px;font-family:arial,sans-serif"> <result property=3D"b=
cd.c" column=3D"c"/></div><div style=3D"font-size:13px;font-family:arial=
,sans-serif">
<result property=3D"bcd.d" column=3D"d"/></div><div style=3D"f=
ont-family:arial,sans-serif;font-size:13px"></resultMap></div><div st=
yle=3D"font-family:arial,sans-serif;font-size:13px"><br></div><div style=3D=
"font-family:arial,sans-serif;font-size:13px">
Jeff Butler</div><div style=3D"font-family:arial,sans-serif;font-size:13px"=
><br></div></div><div><div><br><br><div class=3D"gmail_quote">On Thu, Nov 1=
, 2012 at 2:58 PM, dq <span dir=3D"ltr"><<a>d...@kayak.com</a>></span=
> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">The solution I (don't like) found is to use =
resultMaps with the <constructor> tag, which thanks to <a href=
=3D"http://code.google.com/p/mybatis/issues/detail?id=3D15" target=3D"_blan=
k">http://code.google.com/p/<u></u>my<wbr>batis/issues/detail?id=3D15</a>&n=
bsp;<u></u>can call a constructor on the encompassing object passing in the=
objects constructed in its constituent resultMaps. I'd still prefer =
being able to do it the iBatis way so I don't have to create constructors t=
o satisfy the DAO.</blockquote>
</div><br></div>
</div></blockquote></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</blockquote></div>
------=_Part_842_32113905.1351803749452--
------=_Part_841_16794100.1351803749452--