ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eduardo Shanahan <eduardoshana...@gmail.com>
Subject Problem with mapping of complex class
Date Wed, 21 Mar 2007 09:25:26 GMT
Hi,

I am getting confused about how to map three classes to two tables.

My classes are:


public class Office
    {
        private int _Id;

        public int OfficeId
        {
            get
            {
                return _Id;
            }
            set
            {
                _Id = value;
            }
        }

        private string _Description;

        public string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }
    }



public class Box
    {
       private Office _Office;

       public Office TheOffice
       {
          get
          {
             return _Office;
          }
          set
          {
             _Office = value;
          }
       }
    }



public class Declaration
    {
       private int _Id;

        public int DeclarationId
        {
            get
            {
                return _Id;
            }
            set
            {
                _Id = value;
            }
        }

       private Box _Box;

       public Box TheBox
       {
          get
          {
             return _Box;
          }
          set
          {
             set _Box = value;
          }
       }
    }

I have two tables, one related to the office and the other to the
declaration:

table lkp_office
{
	o_id  int
	,o_code varchar(3)
	,o_description varchar(100)
}

table tbl_declaration
{
	d_id int
	,d_box_ref int
}

where d_box_ref is refering to o_id.

I am sending data to the database for the Office and the Declaration,
but I really need to have the Box as a separate class instead of using
the Office directly in the Declaration.

Then, I am using the following maps:

In OfficeMap.xml:

    <resultMap id="rmOffice" class="Office">
      <result property="OfficeId" column="O_ID"/>
      <result property="Code" column="O_CODE"/>
      <result property="Description" column="O_DESCRIPTION"/>
    </resultMap>

    <select id="OfficeSelectById" parameterClass="int" resultMap="rmOffice">
        select *
        from LKP_OFFICE
        where
        O_ID = #value#
    </select>

and in DeclarationMap.xml:

    resultMap id="rmDeclaration" class="Declaration">
      <result property="DeclarationId" column="D_ID"/>
      <result property="Box.Office" column="D_BOX_REF"
select="OfficeSelectById"/>
    </resultMap>


    <select id="DeclarationSelectAll" resultMap="rmDeclaration">
       select *
       from tbl_Declaration
    </select>

In my test I ask for QueryForList<Declaration>(DeclarationSelectAll,
null) as List<Declaration>. My understanding was that I should get a
list of Declarations which will have the corresponding Office in the Box
property, but instead I am getting the message:

Unable to cast object of type 'Test.Declaration' to type 'Test.Box'

At first I thought that may be iBatis didn't like my Box.Office, but if
I try with

    <result property="Box.Office.OfficeId" column="D_BOX_REF"/>

I get the proper value in the Office.OfficeId, meaning that the problem
is not with the dots

I was thinking about tracing iBatis source code to see what is going on,
but may be someone have seen this problem before, or have an idea about
where is the problem in my maps.

Thanks in advance.

Eduardo Shanahan



Mime
View raw message