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 Fearghal O Maolcatha <fe...@gmail.com> on 2007/10/08 15:11:52 UTC

Handling 0..1 association?

Hi,

I have a class A that may or may not contain a reference to an associated
class B. I'm doing a left outer join between the table being represented by
class A and the table being represented by class B. If there is no match in
the table represented by class B then Ibatis still returns a reference to an
instance of class B with all properties set to their defaults. I would have
expected the reference to be set to null. Is there a way to achieve this
behaviour (having the reference set to null when there is no corresponding
instance), or do I need to write a customer handler?

Regards,
Fearghal.

RE: Handling 0..1 association?

Posted by Clinton Begin <cl...@gmail.com>.
That's weird....I thought for the join mapping that this would work fine.  

 

I thought the only time we had this problem was when lazy loading was
enabled and a separate select query was used....

 

Fearghal, can you post your SQL and result mappings?

 

Clinton

 

From: Fearghal O Maolcatha [mailto:fearghal.omaolcatha@gmail.com] 
Sent: October-08-07 8:45 AM
To: user-java@ibatis.apache.org; lmeadors@apache.org
Subject: Re: Handling 0..1 association?

 

Larry, thanks for the info.

On 08/10/2007, Larry Meadors <lm...@apache.org> wrote:

You'll need to write a custom handler.

The reason is that since you are mapping SQL results to Java object,
iBATIS cannot know that there is no record - all it sees are the
results, not the underlying tables. 

Probably the simplest solution in this case is to do the query as you
are now, but in your DAO, look at the property in B that represents
the PK of table B - if it's null, set B to null.

It's a one liner: 

if(b.getPK() == null) a.setB(null);

Larry


On 10/8/07, Fearghal O Maolcatha <fe...@gmail.com> wrote:
> Hi,
>
> I have a class A that may or may not contain a reference to an associated 
> class B. I'm doing a left outer join between the table being represented
by
> class A and the table being represented by class B. If there is no match
in
> the table represented by class B then Ibatis still returns a reference to
an 
> instance of class B with all properties set to their defaults. I would
have
> expected the reference to be set to null. Is there a way to achieve this
> behaviour (having the reference set to null when there is no corresponding

> instance), or do I need to write a customer handler?
>
> Regards,
> Fearghal.
>
>

 


Re: Handling 0..1 association?

Posted by Fearghal O Maolcatha <fe...@gmail.com>.
Larry, thanks for the info.

On 08/10/2007, Larry Meadors <lm...@apache.org> wrote:
>
> You'll need to write a custom handler.
>
> The reason is that since you are mapping SQL results to Java object,
> iBATIS cannot know that there is no record - all it sees are the
> results, not the underlying tables.
>
> Probably the simplest solution in this case is to do the query as you
> are now, but in your DAO, look at the property in B that represents
> the PK of table B - if it's null, set B to null.
>
> It's a one liner:
>
> if(b.getPK() == null) a.setB(null);
>
> Larry
>
>
> On 10/8/07, Fearghal O Maolcatha <fe...@gmail.com> wrote:
> > Hi,
> >
> > I have a class A that may or may not contain a reference to an
> associated
> > class B. I'm doing a left outer join between the table being represented
> by
> > class A and the table being represented by class B. If there is no match
> in
> > the table represented by class B then Ibatis still returns a reference
> to an
> > instance of class B with all properties set to their defaults. I would
> have
> > expected the reference to be set to null. Is there a way to achieve this
> > behaviour (having the reference set to null when there is no
> corresponding
> > instance), or do I need to write a customer handler?
> >
> > Regards,
> > Fearghal.
> >
> >
>

Re: Handling 0..1 association?

Posted by Larry Meadors <lm...@apache.org>.
You'll need to write a custom handler.

The reason is that since you are mapping SQL results to Java object,
iBATIS cannot know that there is no record - all it sees are the
results, not the underlying tables.

Probably the simplest solution in this case is to do the query as you
are now, but in your DAO, look at the property in B that represents
the PK of table B - if it's null, set B to null.

It's a one liner:

if(b.getPK() == null) a.setB(null);

Larry


On 10/8/07, Fearghal O Maolcatha <fe...@gmail.com> wrote:
> Hi,
>
> I have a class A that may or may not contain a reference to an associated
> class B. I'm doing a left outer join between the table being represented by
> class A and the table being represented by class B. If there is no match in
> the table represented by class B then Ibatis still returns a reference to an
> instance of class B with all properties set to their defaults. I would have
> expected the reference to be set to null. Is there a way to achieve this
> behaviour (having the reference set to null when there is no corresponding
> instance), or do I need to write a customer handler?
>
> Regards,
> Fearghal.
>
>