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 stang <sa...@gmail.com> on 2009/04/07 19:51:00 UTC

Does Ibatis support recursive sql maps?

Hi there,

I'm trying to use IBatis to model a parent-child relationship where i have
some object that has a parent, and that parent has its own parent, and so
on.  Is this supported?  I am having trouble making it work and I suspect
that I may not be doing it right because my results are always null (but no
exceptions).  Below is my example sql map definition that causes the
problem:

  <resultMap id="result" class="someClass">
    <result property="id" column="id"/>
    <result property="parent" column="id" select="getParent"/>
  </resultMap>
  <select id="getParent" parameterClass="string" resultMap="result">
    select f1.id as id from someTable f1,
    (select parentID from someTable
    where id = #value#) f2
    where f1.id = f2.parentID
  </select>

When I call getParent, it would fail given the definition above.  If I
remove the <result property="parent"...> line in "result" resultMap, then it
works.  What I'm trying to do is to lazy-select the parent recursively. 
Does anyone know if IBatis support this concept?



-- 
View this message in context: http://www.nabble.com/Does-Ibatis-support-recursive-sql-maps--tp22934620p22934620.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: Does Ibatis support recursive sql maps?

Posted by "Eric T. Blue" <er...@gmail.com>.
Hi,

I had to do something similar to this a few years ago.  However, I chose to
model that parent-child hierarchy (tree) in code rather than trying to solve
via SQL and iBatis.  In fact, this may be preferable from a performance
standpoint if the number of entities you're trying to model are large
enough.

If you're open to it, I'd recommend updating your query to return all of the
candidate rows from the table.  Then, create a mapping of parent ID to the
actual object and build the tree appropriately.  If you'd like a more
concrete example, I'm sure I can dig up the code somewhere.

On Tue, Apr 7, 2009 at 10:51 AM, stang <sa...@gmail.com> wrote:

>
> Hi there,
>
> I'm trying to use IBatis to model a parent-child relationship where i have
> some object that has a parent, and that parent has its own parent, and so
> on.  Is this supported?  I am having trouble making it work and I suspect
> that I may not be doing it right because my results are always null (but no
> exceptions).  Below is my example sql map definition that causes the
> problem:
>
>  <resultMap id="result" class="someClass">
>    <result property="id" column="id"/>
>    <result property="parent" column="id" select="getParent"/>
>  </resultMap>
>  <select id="getParent" parameterClass="string" resultMap="result">
>    select f1.id as id from someTable f1,
>    (select parentID from someTable
>    where id = #value#) f2
>    where f1.id = f2.parentID
>  </select>
>
> When I call getParent, it would fail given the definition above.  If I
> remove the <result property="parent"...> line in "result" resultMap, then
> it
> works.  What I'm trying to do is to lazy-select the parent recursively.
> Does anyone know if IBatis support this concept?
>
>
>
> --
> View this message in context:
> http://www.nabble.com/Does-Ibatis-support-recursive-sql-maps--tp22934620p22934620.html
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>
>