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 William Shields <wi...@jdv.com> on 2007/10/24 03:48:06 UTC

RowHandler plus groupBy... bug?

Sorry, I sent this once but I wasn't subscribed so didn't know if I needed to send it again:

Perhaps foolishly I'm trying to combine the two.  I have a query that does a groupBy and works well.  For arguments sake, let's call the master table Person and the detail record Album.  A Person owns one or more Albums.  So:

public class Person {
  private int personId;
  private String name;
  private List<Album> albums;
}

public class Album {
  private int albumId;
  private String name;
  private int year;
}

and:

resultMap id="album" class="Album">
  <result property="albumId" column="ALBUM_ID"/>
  <result property="name" column="ALBUM_NAME"/>
  <result property="year" column="YEAR"/>
</resultMap>

<resultMap id="person" class="Person" groupBy="personId">
  <result property="personId" column="PERSON_ID"/>
  <result property="name" column="PERSON_NAME"/>
  <result property="albums" resultMap="album"/>
</resultMap>

<select id="selectPersons" resultMap="person">
  SELECT person.person_id, person_name, album_id, album_name, year
  FROM person, album
  WHERE person.person_id = album.person_id
</select>

(ignoring the realities that it is better modeled as a M:N relationship)

If person A owns 8 albums, queryForList() works great.  However if you use a RowHandler for this query, the size of the albums list is always 1.  Basically the RowHandler seems to be getting called on the master object after a single row has been processed rather than when a break occurs (and the list is full).  Am I right?  Or am I missing something?

Sounds like a bug to me.  I could ditch the groupBy and use the RowHandler only but I'm dealing with a large result set and I'd otherwise have to build the complete object myself.  Unfortunately, there doesn't seem to be a way to effectively delay processing until all list has been built.  The RowHandler could use a lastValue property to manually calculate a break but that won't get triggered after thelast row is processed (and a RowHandler doesn't know when something is the last row).

Any suggestions?

------------------------------------------------------------------------------------------------------------------------------
This e-mail and any files transmitted with it are confidential and are only for the use of the person to whom they are addressed. If you are not the intended recipient, you are hereby notified that any use, dissemination, forwarding, printing, copying or dealing in any way whatsoever with this e-mail is strictly prohibited. If you have received this e-mail in error, please reply to us immediately and delete the document.
It is the recipient's duty to virus-scan and otherwise test the enclosed information before using the information or loading attached files onto any computer system. JDV Limited does not warrant that the information contained in this e-mail is free from viruses, defects, errors, interception or interference.
JDV Limited, and each of its related companies each reserve the right to monitor all e-mail communications through its networks. 
Any views expressed in this message are those of the individual sender, except where that sender specifically states them to be the views of JDV Limited.
Your private information is only used and disclosed for the intention which you have provided it for. This information is not disclosed or used unless your consent has been provided or in the case that JDV Limited is permitted to do so under the Privacy Act of 1988.
-----------------------------------------------------------------------------------------------------------------------------

RE: RowHandler plus groupBy... bug?

Posted by William Shields <wi...@jdv.com>.
I've run another test on this.  I wrote a RowHandler that (much like the DefaultRowHandler), simply puts the row into a list.  On each invocation of handleRow(), the sub-list is size 1.  It comes out at the correct size (if you look at the list after the fact).

This, to me, sounds like a bug.

-----Original Message-----
From: William Shields [mailto:william_shields@jdv.com]
Sent: Wed 10/24/2007 9:48 AM
To: user-java@ibatis.apache.org
Subject: RowHandler plus groupBy... bug?
 

Sorry, I sent this once but I wasn't subscribed so didn't know if I needed to send it again:

Perhaps foolishly I'm trying to combine the two.  I have a query that does a groupBy and works well.  For arguments sake, let's call the master table Person and the detail record Album.  A Person owns one or more Albums.  So:

public class Person {
  private int personId;
  private String name;
  private List<Album> albums;
}

public class Album {
  private int albumId;
  private String name;
  private int year;
}

and:

resultMap id="album" class="Album">
  <result property="albumId" column="ALBUM_ID"/>
  <result property="name" column="ALBUM_NAME"/>
  <result property="year" column="YEAR"/>
</resultMap>

<resultMap id="person" class="Person" groupBy="personId">
  <result property="personId" column="PERSON_ID"/>
  <result property="name" column="PERSON_NAME"/>
  <result property="albums" resultMap="album"/>
</resultMap>

<select id="selectPersons" resultMap="person">
  SELECT person.person_id, person_name, album_id, album_name, year
  FROM person, album
  WHERE person.person_id = album.person_id
</select>

(ignoring the realities that it is better modeled as a M:N relationship)

If person A owns 8 albums, queryForList() works great.  However if you use a RowHandler for this query, the size of the albums list is always 1.  Basically the RowHandler seems to be getting called on the master object after a single row has been processed rather than when a break occurs (and the list is full).  Am I right?  Or am I missing something?

Sounds like a bug to me.  I could ditch the groupBy and use the RowHandler only but I'm dealing with a large result set and I'd otherwise have to build the complete object myself.  Unfortunately, there doesn't seem to be a way to effectively delay processing until all list has been built.  The RowHandler could use a lastValue property to manually calculate a break but that won't get triggered after thelast row is processed (and a RowHandler doesn't know when something is the last row).

Any suggestions?

------------------------------------------------------------------------------------------------------------------------------
This e-mail and any files transmitted with it are confidential and are only for the use of the person to whom they are addressed. If you are not the intended recipient, you are hereby notified that any use, dissemination, forwarding, printing, copying or dealing in any way whatsoever with this e-mail is strictly prohibited. If you have received this e-mail in error, please reply to us immediately and delete the document.
It is the recipient's duty to virus-scan and otherwise test the enclosed information before using the information or loading attached files onto any computer system. JDV Limited does not warrant that the information contained in this e-mail is free from viruses, defects, errors, interception or interference.
JDV Limited, and each of its related companies each reserve the right to monitor all e-mail communications through its networks. 
Any views expressed in this message are those of the individual sender, except where that sender specifically states them to be the views of JDV Limited.
Your private information is only used and disclosed for the intention which you have provided it for. This information is not disclosed or used unless your consent has been provided or in the case that JDV Limited is permitted to do so under the Privacy Act of 1988.
-----------------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------------
This e-mail and any files transmitted with it are confidential and are only for the use of the person to whom they are addressed. If you are not the intended recipient, you are hereby notified that any use, dissemination, forwarding, printing, copying or dealing in any way whatsoever with this e-mail is strictly prohibited. If you have received this e-mail in error, please reply to us immediately and delete the document.
It is the recipient's duty to virus-scan and otherwise test the enclosed information before using the information or loading attached files onto any computer system. JDV Limited does not warrant that the information contained in this e-mail is free from viruses, defects, errors, interception or interference.
JDV Limited, and each of its related companies each reserve the right to monitor all e-mail communications through its networks. 
Any views expressed in this message are those of the individual sender, except where that sender specifically states them to be the views of JDV Limited.
Your private information is only used and disclosed for the intention which you have provided it for. This information is not disclosed or used unless your consent has been provided or in the case that JDV Limited is permitted to do so under the Privacy Act of 1988.
-----------------------------------------------------------------------------------------------------------------------------