3

NHibernate を使用してデータを読み取りたいレガシー データベースがあります。マップするテーブルは次のとおりです。

ユーザー

  • 主キー - ユーザー ID
  • PK - グループ ID
  • ロケーションソース
  • 等...

場所

  • 主キー - ユーザー ID
  • PK - グループ ID
  • PK - ソース
  • バツ

すべてのユーザーには、1 つ以上の場所があります。場所は、列によって識別されるさまざまなソースから入力できSourceます。Users テーブルの LocationSource列には、そのユーザーに最も関連性の高いロケーション ソースが保持されます。

現在作成中のアプリケーションでは、最後の場所のみが必要です。これは主にパフォーマンス上の理由から行われます。ユーザーをロードするたびに (外部結合を使用して) すべての場所をロードしたくありません (遅延ロードも問題外です)。

クラスは次のようになります。

public class UserKey
{
    public int UserId {get;set;}
    public int GroupId {get;set;
}

public class Location
{
    public double X {get;set;}
    public double Y {get;set;}
    public LocationSource Source {get;set;}
}

public class User
{
    public UserKey Id {get; set;}
    public Location Location {get;set;}
}

データベーススキームをそれらのクラスにマップする方法がわかりません。これまで試したことはすべて失敗しました。

私はあなたの助けに感謝します。

4

1 に答える 1

0

これが私がそれを行う方法です。

ユーザーには、リスト (場所) と現在の場所 (ソース) への参照の両方が含まれるため、ユーザーごとの現在の場所と、ユーザーの場所の履歴リストが取得されます。

User.Locations と User.Source はどちらもデフォルトで遅延ロードされますが、任意のクエリ オプションを使用して User.Source を積極的にロードし、現在の場所を取得することができます。

Locations プロパティを介して User に Location を追加する場合、明らかに Source 参照も管理する必要があります。

XML マッピング ファイルが必要な場合は、Fluent NHibernate 1.1 を使用しただけでなく、提供することもできます。

public class User
{
    public virtual int UserId { get; set; }
    public virtual int GroupId { get; set; }
    public virtual IList<Location> Locations { get; set; }
    public virtual Location Source { get; set; }
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as User;
        if (t == null)
            return false;
        if (UserId == t.UserId && GroupId == t.GroupId)
            return true;
        return false;
    }
    public override int GetHashCode()
    {
        return (UserId + "|" + GroupId).GetHashCode();
    }
}

public class Source
{
    public virtual int Id { get; set; }
}

public class Location
{
    public virtual User User { get; set; }
    public virtual int Id { get; set; }
    public virtual Source Source { get; set; } 
    public virtual string X { get; set; }
    public virtual string Y { get; set; }
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as Location;
        if (t == null)
            return false;
        if (User == t.User && Source == t.Source)
            return true;
        return false;
    }
    public override int GetHashCode()
    {
        return (User.GetHashCode() + "|" + Id).GetHashCode();
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        CompositeId()
            .KeyProperty(x => x.UserId, "UserId")
            .KeyProperty(x => x.GroupId, "GroupId");
        HasMany(x => x.Locations);
        References(x => x.Source).Columns("UserId", "GroupId", "LocationSource");
    }
}

public class LocationMap : ClassMap<Location>
{
    public LocationMap()
    {
        CompositeId()
            .KeyReference(x => x.Source, "Source")
            .KeyReference(x => x.User,"groupId","userid");
        References(x => x.User).Columns("userid","groupid");
    }
}

public class SourceMap : ClassMap<Source>
{
    public SourceMap()
    {
        Id(x => x.Id).GeneratedBy.Native();
    }
}
于 2011-05-23T12:57:46.580 に答える