はじめてのS2Dao.NETその2

クラスとテーブルが1対1の場合は、上手く行くことが分かったので、N対1も試してみました。
実験用にテーブルを作成します。

create table Parent (
    id  integer,
    name varchar (32)
);

create table Child (
    id  integer,
    pid integer,
    name varchar (32)
);

create table Grandchild (
    id  integer,
    cid integer,
    name varchar (32)
);

alter table Parent add constraint Parent_PK primary key (id);
alter table Child add constraint Child_PK primary key (id);
alter table Grandchild add constraint Grandchild_PK primary key (id);

alter table Child add constraint Child_FK foreign key (pid) references Parent (id);
alter table Grandchild add constraint Grandchild_FK foreign key (cid) references Child (id);

見ての通り、親、子、孫関係のテーブル群です。テーブルと1対1のエンティティとは別にN対1用のDtoを用意します。(Parentのコードは省略)

using System;
using Seasar.Dao.Attrs;

[Table("Child")]
public class ChildDto {
    private int id;
    private string name;
    // ParentはEntity
    private Parent pid;

    public int ID {
        get { return id; }
        set { id = value; }
    }

    public string Name {
        get { return name; }
        set { name = value; }
    }

    [Relno (0)]
    [Relkeys ("PID:ID")]
    public Parent PID {
        get { return pid; }
        set { pid = value; }
    }

    public override string ToString () {
        return String.Format ("{0}:{1}", ID, Name);
    }
}

[Table("Grandchild")]
public class GrandchildDto {
    private int id;
    private string name;
    private ChildDto cid;

    public int ID {
        get { return id; }
        set { id = value; }
    }

    public string Name {
        get { return name; }
        set { name = value; }
    }

    [Relno (0)]
    [Relkeys ("CID:ID")]
    public ChildDto CID {
        get { return cid; }
        set { cid = value; }
    }

    public override string ToString () {
        return String.Format ("{0}:{1}", ID, Name);
    }
}

Relno属性とRelkeys属性によってテーブルのリンクを行っています。Relkeys属性は「自分のテーブルのカラム:相手のテーブルのカラム」という指定になっています。

あとは、

using System;
using System.Collections;
using Seasar.Dao.Attrs;

[Bean (typeof (GrandchildDto))]
public interface IGrandchildDtoDao {
    IList GetAllList ();

    [Query ("id=/*id*/")]
    Grandchild GetGrandchildDtoByID (int id);
}

こんなDaoを用意してデータを取得です。・・・が、孫から子をリンクすることは出来ましたが子の親はnullでした。孫->子、孫->親という2つの参照にしないと無理なのかな?