You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Eduardo Shanahan <ed...@gmail.com> on 2007/03/21 10:25:26 UTC

Problem with mapping of complex class

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



Re: Problem with mapping of complex class

Posted by Jakub Scheibe <ja...@gmail.com>.
I have the same problem.
I debug the code and as far as I can see it's crashing when it's trying
to cast the data. The resultmap that I used is selecting proper data
from the data base, but when it's trying to cast new class to the
target class it's showing exception. If instead of declaration fe.
Class1.Class2 i will use Class1.Class2.Id then I'm getting proper Id
but I want to fill all the properties with the data, not only ID.
Gilles/anyone can u please take a look at this?? I described exactly the
same problem previously but Eddie done this better.

Regards,
Jakub.