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 "Tom B." <to...@gmail.com> on 2005/07/18 16:02:25 UTC

Nested Beans not populating using SQL alias.

I saw this question had been asked earlier, with no reply, so I'll try
and re-phrase ;-)

I have a nested bean called "Bar", inside of a class "Foo".  I am able
to use a Map to populate this bean, IE:

<result property="bar.baz1" column="baz1" />
<result property="bar.baz2" column="baz2" />

However, when I use the "equivelant" SQL alias without a map:

SELECT
     baz1 AS "bar.baz1",
     baz2 AS "bar.baz2"
FROM
     foo

This SQL does not populate my nested bean.  Is this a bug?

My Classes would look like:

class Foo{
   private Bar bar;

   Bar getBar() {
     if(this.bar == null) {
       this.bar = new Bar();
     }
     return bar;
   }
   void setBar(Bar bar) {
     this.bar = bar;
   }
}
....
class Bar{
    String getBaz1() {}
    void setBaz1(String baz1) {}
    String getBaz2() {}
    void setBaz2(String baz2) {}
}
....


Thanks!
Tom

Re: Nested Beans not populating using SQL alias.

Posted by "Tom B." <to...@gmail.com>.
Your solution would work, provided that the getters and setters are in
the base class "Foo".   The methods I'm selecting into are nested, not
simple properties, in the class "Bar".

Here's a better example:

<select id="test" resultClass="Foo">
SELECT
   baz1 AS "bar.baz1",
   baz2 AS "bar.baz2",
   column1 AS "test"
FROM
   foo
</select>

class Foo{
 private Bar bar;
 private String test;

 Bar getBar() {
   if(this.bar == null) {
     this.bar = new Bar();
   }
   return bar;
 }
 void setBar(Bar bar) {
   this.bar = bar;
 }

 void setTest(String test) {
   this.test = test;
 }

 String getTest() {
   return this.test;
 }
}

....
class Bar{
  private String baz1, baz2;

  String getBaz1() {
    return baz1;
  }
  void setBaz1(String baz1) {
     this.baz1= baz1;
   }
  String getBaz2() {
    return baz2;
  }
  void setBaz2(String baz2) {
    this.baz2 = baz2;
  }
}
....

In this example, member "test" in class "Foo" is set correctly...
however, the values of nested member "baz1" and "baz2" in the bean
"Bar" are not set.  If I use a Map instead of relying on the SQL
alias:

<result property="test" column="column1" />
<result property="bar.baz1" column="baz1" />
<result property="bar.baz2" column="baz2" />

 all values are set correctly, even the nested ones.

This is my hang-up... let me know if there is a workaround or I'm
missing something.

Tom

Re: Nested Beans not populating using SQL alias.

Posted by Zarar Siddiqi <za...@utoronto.ca>.
Your column names aren't matching. Try this instead.
 SELECT
     baz1 
     baz2 
 FROM
     foo

Also, make sure the Bar class has a get/set for baz1 and baz2.

Zarar

----- Original Message ----- 
From: "Tom B." <to...@gmail.com>
To: <us...@ibatis.apache.org>
Sent: Monday, July 18, 2005 10:02 AM
Subject: Nested Beans not populating using SQL alias.


>I saw this question had been asked earlier, with no reply, so I'll try
> and re-phrase ;-)
> 
> I have a nested bean called "Bar", inside of a class "Foo".  I am able
> to use a Map to populate this bean, IE:
> 
> <result property="bar.baz1" column="baz1" />
> <result property="bar.baz2" column="baz2" />
> 
> However, when I use the "equivelant" SQL alias without a map:
> 
> SELECT
>     baz1 AS "bar.baz1",
>     baz2 AS "bar.baz2"
> FROM
>     foo
> 
> This SQL does not populate my nested bean.  Is this a bug?
> 
> My Classes would look like:
> 
> class Foo{
>   private Bar bar;
> 
>   Bar getBar() {
>     if(this.bar == null) {
>       this.bar = new Bar();
>     }
>     return bar;
>   }
>   void setBar(Bar bar) {
>     this.bar = bar;
>   }
> }
> ....
> class Bar{
>    String getBaz1() {}
>    void setBaz1(String baz1) {}
>    String getBaz2() {}
>    void setBaz2(String baz2) {}
> }
> ....
> 
> 
> Thanks!
> Tom
>