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 Stefan Langer <ma...@googlemail.com> on 2006/11/29 17:13:16 UTC

How to handle null objects in nested ResultMaps

Hello,

I new to IBatis and for a small project of mine I'm using only the 
datamapper framework from Ibatis.

I have a fairly simple data structure where a notebook contains several 
pages.

For simplicity assume this domainmodel:

Notebook           Page
   [id]     1 -> N   [id]

The corresponding Map looks like this ( this is not a complete example 
as I only want to make clear what my situation is)

<resultMap class="notebook" groupBy="id" id="notebookResult">
    <result column="NB_ID" jdbcType="VARCHAR" property="id"/>
    <result javaType="java.util.List" property="pages" 
resultMap="Note.pageResult"/>
</resultMap>

<resultMap class="page" groupBy="id" id="pageResult">
   <result column="P_ID" jdbcType="VARCHAR" property="id"/>
</resultMap>

I'm using the following select statement to get the notebooks and 
corresponding pages

<select id="selectNotebook" parameterClass="java.lang.String" 
resultMap="notebookResult">
SELECT nb.id as nb_id, p.id as p_id, 	
FROM notebook nb LEFT JOIN page p ON nb.id = p.notebook_id
WHERE nb.id = #id#
</select>

When I have a notebook that has no pages IBatis sets a page object with 
ID = null in the pages property of notebook.
I need to get notebooks that do not have pages but in those cases I do 
not wish to have a page object at all.

Is there an easy way around this situations? I have tried to Google and 
looked at the archive but all the threads that I found dealing with this 
didn't give me any information on how this is solved.

Thanks for any help

Regards
Stefan


Re: How to handle null objects in nested ResultMaps

Posted by Stefan Langer <ma...@googlemail.com>.
Larry Meadors wrote:
> On 11/30/06, Stefan Langer <ma...@googlemail.com> wrote:
>> I wonder if this is a feature other people might want so that it is
>> worth filing an enhancement request. I'm thinking along the lines of a
>> special flag on the result node that specifies whether to create an
>> object if a specified property is null or not. Since I'm very new to
>> IBATIS I'm lacking the insight to know wether this is even doable or
>> whether not.
>
> Yeah, please do enter it in JIRA as an enhancement.
>
> Larry
>
Done the issue key is  IBATIS-375

Stefan

Re: How to handle null objects in nested ResultMaps

Posted by Stefan Langer <ma...@googlemail.com>.
Larry Meadors wrote:
> On 11/30/06, Stefan Langer <ma...@googlemail.com> wrote:
>> I wonder if this is a feature other people might want so that it is
>> worth filing an enhancement request. I'm thinking along the lines of a
>> special flag on the result node that specifies whether to create an
>> object if a specified property is null or not. Since I'm very new to
>> IBATIS I'm lacking the insight to know wether this is even doable or
>> whether not.
>
> Yeah, please do enter it in JIRA as an enhancement.
>
> Larry
>
The Issue is https://issues.apache.org/jira/browse/IBATIS-375 go vote for it

Regards
Stefan

Re: How to handle null objects in nested ResultMaps

Posted by Larry Meadors <lm...@apache.org>.
On 11/30/06, Stefan Langer <ma...@googlemail.com> wrote:
> I wonder if this is a feature other people might want so that it is
> worth filing an enhancement request. I'm thinking along the lines of a
> special flag on the result node that specifies whether to create an
> object if a specified property is null or not. Since I'm very new to
> IBATIS I'm lacking the insight to know wether this is even doable or
> whether not.

Yeah, please do enter it in JIRA as an enhancement.

Larry

Re: How to handle null objects in nested ResultMaps

Posted by Stefan Langer <ma...@googlemail.com>.
Larry Meadors wrote:
> On 11/30/06, Stefan Langer <ma...@googlemail.com> wrote:
>> Larry Meadors wrote:
>> > Not really, no.
>> >
>> > If you run this SQL in a database tool, it may become apparent
>> > why..there is data there, it's just null. So iBATIS creates that
>> > data...even if it is null.
>> Well the data is there because I'm doing a left join and there are no
>> pages. This I understand. What I'm interested in knowing is if IBatis
>> provides a mean to make this be handeled transparently while still
>> handling the N+1 Problem.
>
> No, it does not currently do that.
>
>> >
>> > The quick solution is to look at the data returned, and remove that
>> > data if you do not want it there.
>> Is there someway I can do this through IBatis, like TypeHandler or
>> something like that, or do I have to do this on my own in my 
>> Domainmodels?
>
> You might look at the RowHandler interface. It would let you do this,
> too, but would probably be more work.
>
>> >
>> > A better solution might be to add functionality to iBATIS to do this,
>> > so you could tell it to look at a field and if it was null, to skip
>> > the population of the child list.
>>  From this I gather that IBatis does not provide such a functionality
>> and I will have to dig in and provide it myself? Am I correct?
>
> You are correct, yes.
>
> Larry
>
Thanks for the information. I handled the problem by implementing a 
wrapper for List which basically ignores all objects that have a 
primarkey that is null. It seemed to me more easily realizable then the 
RowHandler interface. Especially since I didn't see how this interface 
is used with a nested resultmap.
I wonder if this is a feature other people might want so that it is 
worth filing an enhancement request. I'm thinking along the lines of a 
special flag on the result node that specifies whether to create an 
object if a specified property is null or not. Since I'm very new to 
IBATIS I'm lacking the insight to know wether this is even doable or 
whether not.

Stefan

Re: How to handle null objects in nested ResultMaps

Posted by Larry Meadors <lm...@apache.org>.
On 11/30/06, Stefan Langer <ma...@googlemail.com> wrote:
> Larry Meadors wrote:
> > Not really, no.
> >
> > If you run this SQL in a database tool, it may become apparent
> > why..there is data there, it's just null. So iBATIS creates that
> > data...even if it is null.
> Well the data is there because I'm doing a left join and there are no
> pages. This I understand. What I'm interested in knowing is if IBatis
> provides a mean to make this be handeled transparently while still
> handling the N+1 Problem.

No, it does not currently do that.

> >
> > The quick solution is to look at the data returned, and remove that
> > data if you do not want it there.
> Is there someway I can do this through IBatis, like TypeHandler or
> something like that, or do I have to do this on my own in my Domainmodels?

You might look at the RowHandler interface. It would let you do this,
too, but would probably be more work.

> >
> > A better solution might be to add functionality to iBATIS to do this,
> > so you could tell it to look at a field and if it was null, to skip
> > the population of the child list.
>  From this I gather that IBatis does not provide such a functionality
> and I will have to dig in and provide it myself? Am I correct?

You are correct, yes.

Larry

Re: How to handle null objects in nested ResultMaps

Posted by Stefan Langer <ma...@googlemail.com>.
Larry Meadors wrote:
> Not really, no.
>
> If you run this SQL in a database tool, it may become apparent
> why..there is data there, it's just null. So iBATIS creates that
> data...even if it is null.
Well the data is there because I'm doing a left join and there are no 
pages. This I understand. What I'm interested in knowing is if IBatis 
provides a mean to make this be handeled transparently while still 
handling the N+1 Problem.
>
> The quick solution is to look at the data returned, and remove that
> data if you do not want it there.
Is there someway I can do this through IBatis, like TypeHandler or 
something like that, or do I have to do this on my own in my Domainmodels?
>
> A better solution might be to add functionality to iBATIS to do this,
> so you could tell it to look at a field and if it was null, to skip
> the population of the child list.
 From this I gather that IBatis does not provide such a functionality 
and I will have to dig in and provide it myself? Am I correct?

Thanks for your help

Regards
Stefan

Re: How to handle null objects in nested ResultMaps

Posted by Larry Meadors <lm...@apache.org>.
Not really, no.

If you run this SQL in a database tool, it may become apparent
why..there is data there, it's just null. So iBATIS creates that
data...even if it is null.

The quick solution is to look at the data returned, and remove that
data if you do not want it there.

A better solution might be to add functionality to iBATIS to do this,
so you could tell it to look at a field and if it was null, to skip
the population of the child list.

Larry


On 11/29/06, Stefan Langer <ma...@googlemail.com> wrote:
> Hello,
>
> I new to IBatis and for a small project of mine I'm using only the
> datamapper framework from Ibatis.
>
> I have a fairly simple data structure where a notebook contains several
> pages.
>
> For simplicity assume this domainmodel:
>
> Notebook           Page
>    [id]     1 -> N   [id]
>
> The corresponding Map looks like this ( this is not a complete example
> as I only want to make clear what my situation is)
>
> <resultMap class="notebook" groupBy="id" id="notebookResult">
>     <result column="NB_ID" jdbcType="VARCHAR" property="id"/>
>     <result javaType="java.util.List" property="pages"
> resultMap="Note.pageResult"/>
> </resultMap>
>
> <resultMap class="page" groupBy="id" id="pageResult">
>    <result column="P_ID" jdbcType="VARCHAR" property="id"/>
> </resultMap>
>
> I'm using the following select statement to get the notebooks and
> corresponding pages
>
> <select id="selectNotebook" parameterClass="java.lang.String"
> resultMap="notebookResult">
> SELECT nb.id as nb_id, p.id as p_id,
> FROM notebook nb LEFT JOIN page p ON nb.id = p.notebook_id
> WHERE nb.id = #id#
> </select>
>
> When I have a notebook that has no pages IBatis sets a page object with
> ID = null in the pages property of notebook.
> I need to get notebooks that do not have pages but in those cases I do
> not wish to have a page object at all.
>
> Is there an easy way around this situations? I have tried to Google and
> looked at the archive but all the threads that I found dealing with this
> didn't give me any information on how this is solved.
>
> Thanks for any help
>
> Regards
> Stefan
>
>