簡體   English   中英

流利的Nhibernate:關鍵引用具有CompositeId的實體

[英]Fluent Nhibernate: key-referencing an entity with a compositeId

在映射這些關系時,我遇到了Fluent Nhibernate問題:

請求實體:

  • ID
  • 日期

學校實體:

  • 復合ID由
    • 名稱
    • 請求
  • 屬性

位置實體:

  • 復合ID由
    • 存在(布爾值)
    • 學校(-> school_name和request_id上的外鍵)

基本上,這些關系代表了一種情況,其中我向學校詢問一些Web api服務。 我想保存請求(我只在這里輸入日期,但是我還有很多字段)和為學校檢索的信息。 我也想將此信息與生成信息的請求綁定。 我還想保存學校位置並像以前一樣綁定此信息。

到目前為止,我所做的是:

School.cs:

public class School
{
    public virtual string name {get; set;}
    public virtual Request request {get; set;}
    public virtual string property;
}

Request.cs:

public class Request
{
   public virtual int id {get; set;}
   public virtual DateTime date {get; set;}
}

Location.cs:

public class Location
{
    public virtual bool exist {get; set;}
    public virtual School school {get; set;}
    public virtual string province {get; set;}
}

SchoolMap.cs

public class SchoolMap : ClassMap<School>
{
    public SchoolMap() {
        CompositeId().KeyProperty(x => x.name, "name")
                     .KeyProperty(x => x.request, "request_id");
        Map(x => x.property);
    }
}

RequestMap.cs

public class Request : ClassMap<Request>
{
    public SchoolMap() {
        Id(x => x.id).GeneratedBy.Identity();
        Map(x => x.date);
    }
}

LocationMap.cs

public class LocationMap : ClassMap<Location>
{
    public LocationMap() {
        CompositeId().KeyProperty(x => x.exist)
                     .KeyReference(x => x.school, new string[] {"name", "request_id"});

        References<School>(x => x.school).Columns(new string [] {"name", "request_id"}).Cascade.All();
        Map(x => x.province);
    }
}

我還實現了從AbstractRepository繼承的每個存儲庫。 “保存”方法為(例如SchoolRepository的示例)

public School saveSchool(School school)
{
    var transaction = openTransaction();
    session.saveOrUpdate(school);
    transaction.commit();
    return school;
}

該方案不起作用。 如果我創建並保存(按此順序)一個請求,然后是一個引用該請求的學校,然后是一個引用該學校的位置,則我將收到“ SqlParameter OutOfBound”異常。

我不知道如何映射這種關系。

在此先感謝所有人。

我嘗試演示一些復合id釋放。

    public class EntityA
    {
        public EntityA()
        {
            Id = new EntityAId();
        }

        public virtual EntityAId Id { get; protected set; }
    }

    public class EntityAMap : FluentNHibernate.Mapping.ClassMap<EntityA>
    {
        public EntityAMap()
        {
            CompositeId(t => t.Id).KeyProperty(t => t.Id).CustomType<int>();
        }
    }

    public class EntityB
    {
        private EntityA _EntityA;

        public EntityB()
        {
            Id = new EntityBId();
        }

        public virtual EntityBId Id { get; protected set; }

        public virtual int EntityA_Id { get { return Id.EntityA_Id; } protected set { Id.EntityA_Id = value; } }
        public virtual EntityA EntityA { get { return _EntityA; } set { _EntityA = value; EntityA_Id = value == null ? 0 : value.Id.Id; } }
    }

    public class EntityBMap : FluentNHibernate.Mapping.ClassMap<EntityB>
    {
        public EntityBMap()
        {
            CompositeId(t => t.Id).KeyProperty(t => t.Id).CustomType<int>()
                                  .KeyProperty(t => t.EntityA_Id).CustomType<int>();

            Map(t => t.EntityA_Id).Not.Insert().Not.Update();
            References(t => t.EntityA).Columns("EntityA_Id");
        }
    }

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

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            var t = obj as EntityAId;
            if (t == null)
                return false;
            if (Id == t.Id)
                return true;
            return false;
        }
        public override int GetHashCode()
        {
            return (Id).GetHashCode();
        }
    }

    public class EntityBId
    {
        public virtual int EntityA_Id { get; set; }
        public virtual int Id { get; set; }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            var t = obj as EntityBId;
            if (t == null)
                return false;
            if (EntityA_Id == t.EntityA_Id && Id == t.Id)
                return true;
            return false;
        }
        public override int GetHashCode()
        {
            return (EntityA_Id + "|" + Id).GetHashCode();
        }
    }
public class EntityC
{
    private EntityA _EntityA;
    private EntityB _EntityB;

    public EntityC()
    {
        Id = new EntityCId();
    }

    public virtual EntityCId Id { get; protected set; }

    public virtual int EntityA_Id { get { return Id.EntityA_Id; } protected set { Id.EntityA_Id = value; } }
    public virtual EntityA EntityA { get { return _EntityA; } set { _EntityA = value; EntityA_Id = value == null ? 0 : value.Id.Id; } }

    public virtual int EntityB_Id { get { return Id.EntityB_Id; } protected set { Id.EntityB_Id = value; } }
    public virtual EntityB EntityB { get { return _EntityB; } set { _EntityB = value; EntityB_Id = value == null ? 0 : value.Id.Id; } }
}

public class EntityCMap : FluentNHibernate.Mapping.ClassMap<EntityC>
{
    public EntityCMap()
    {
        CompositeId(t => t.Id).KeyProperty(t => t.Id).CustomType<int>()
                              .KeyProperty(t => t.EntityA_Id).CustomType<int>()
                              .KeyProperty(t => t.EntityB_Id).CustomType<int>();

        Map(t => t.EntityA_Id).Not.Insert().Not.Update();
        References(t => t.EntityA).Columns("EntityA_Id");

        Map(t => t.EntityB_Id).Not.Insert().Not.Update();
        References(t => t.EntityB).Columns("EntityA_Id", "EntityB_Id");
    }
}
public class EntityCId
{
    public virtual int EntityA_Id { get; set; }
    public virtual int EntityB_Id { get; set; }

    public virtual int Id { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as EntityCId;
        if (t == null)
            return false;
        if (EntityA_Id == t.EntityA_Id && EntityB_Id == t.EntityB_Id && Id == t.Id)
            return true;
        return false;
    }
    public override int GetHashCode()
    {
        return (EntityA_Id + "|" + EntityB_Id + "|" + Id).GetHashCode();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM