使用具有完整性约束的HasMany关系上的复合ID在Fluent NHibernate中进行级联删除

时间:2012-07-31 20:09:33

标签: nhibernate fluent-nhibernate nhibernate-mapping nhibernate-cascade

我有一个错误,无论我有多少谷歌我都找不到它的修复方法。 我已阅读并尝试使用以下主题解决它。这些是我发现的与我的问题最相关的:

DB Schema

我的地图涉及四个表:MemberVehicleMemberVehiclesLastUsedVehicles

  • MemberVehicle是应用程序中的业务对象。
  • MemberVehicles是一个没有额外列的关联表(many-to-many)。这种关系没有问题。
  • LastUsedVehicles有一个由。组成的复合键 Vehicle_uidMember_uid,它们是相应表的外键。该表还有一个额外的DateLastUsed列来存储时间戳。 这个的映射 表是我的问题。

问题

当我删除Member时,我还需要删除所有Vehicle(这样可以正常工作 - 从多对多表中删除记录),以及所有相关的LastUsedVehicles记录(这是不起作用的部分)。

当我执行此代码时......

    try
    {
        using (IUnitOfWork unitOfWork = unitOfWorkFactory.Create())
        {
            Member member = nHibernateMemberRepository.GetMemberBySomeMethod(x,y)
            if (member != null)
            {
                if (nHibernateMemberRepository.Delete(unitOfWork, member))
                {
                    unitOfWork.Commit();
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }

...我收到此错误:

  

System.Exception:在UnitOfWork失败期间尝试提交;   由于以下错误,事务已回滚:给定密钥   字典中没有出现。 ---->   System.Collections.Generic.KeyNotFoundException:给定的密钥是   不在字典中。

LastUsedVehicles表为空时,我没有收到错误。

映射

会员

public class MemberMap : ClassMap<Member>
{
    public MemberMap()
    {
        Table("member");

        HasManyToMany<Vehicle>(x => x.Vehicles) // working fine
            .Table("member_vehicles")
            .ParentKeyColumn("MEMBER_UID")
            .ChildKeyColumn("VEHICLE_UID")
            .Inverse()
            .Cascade.SaveUpdate()
            .Cascade.AllDeleteOrphan();

        HasMany(x => x.LastUsedVehicleses)
            //.Table("LASTUSED_VEHICLES")
            .KeyColumn("MEMBER_UID")
            .Fetch.Select()
            //.KeyColumns.Add("VEHICLE_UID")
            //.KeyColumns.Add("MEMBER_UID")
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

车辆

    public class VehicleMap : ClassMap<Vehicle>
    {
        public VehicleMap()
        {
            Table("vehicles");

            HasManyToMany(x => x.Members) // working fine
                .Table("member_vehicles")
                .ParentKeyColumn("VEHICLE_UID")
                .ChildKeyColumn("MEMBER_UID")
                .Not.LazyLoad()
                .Cascade.SaveUpdate();

            HasMany(x => x.LastUsedVehicles)
                .KeyColumn("VEHICLE_UID")
                .Inverse()
                .Fetch.Select()
                .Cascade.AllDeleteOrphan();

        }
    }

LastUsedVehicle

    public class LastUsedVehiclesMap : ClassMap<LastUsedVehicles>
    {
        public LastUsedVehiclesMap()
        {
            Table("lastused_vehicles");

            CompositeId()
                .KeyReference(x => x.Vehicle, "VEHICLE_UID")
                .KeyReference(x => x.Member, "MEMBER_UID");

            Map(x => x.DateLastUsed)
                .Column("DATELASTUSED")
                .Not.Nullable();

            // Things that I tried
            //References(x => x.Member)
            //    .Fetch.Select()
            //    .Cascade.None()
            //    .Column("MEMBER_UID");
            //
            //References(x => x.Vehicle)
            //    .Fetch.Select()
            //    .Cascade.None()
            //    .Column("VEHICLE_UID");

            //Version(x => x.DateLastUsed)
            //    .Column("DATELASTUSED")
            //    .Not.Nullable();

        }
    }

会员

public class Member : PrimaryKeyBase
{
    private readonly Iesi.Collections.Generic.ISet<Vehicle> _vehicles;
    private Iesi.Collections.Generic.ISet<LastUsedVehicles> _lastUsedVehicles;

    public Member()
    {
        _vehicles = new HashedSet<Vehicle>();
        _lastUsedVehicles = new HashedSet<LastUsedVehicles>();
    }

    // ... GetHashCode ...

    public virtual Iesi.Collections.Generic.ISet<Vehicle> Vehicles
    {
        get { return _vehicles; }
    }

    public virtual void AddVehicle(Vehicle vehicle)
    {
        vehicle.AddMember(this);
        _vehicles.Add(vehicle);
    }

    public virtual Iesi.Collections.Generic.ISet<LastUsedVehicles> LastUsedVehicleses
    {
        get { return _lastUsedVehicles; }
        set { _lastUsedVehicles = value; }
    }
}

车辆

public class Vehicle : PrimaryKeyBase
{
    private Iesi.Collections.Generic.ISet<Member> _members;
    private Iesi.Collections.Generic.ISet<LastUsedVehicles> _lastUsedVehicles;

    public Vehicle()
    {
        _members = new HashedSet<Member>();
        _lastUsedVehicles = new HashedSet<LastUsedVehicles>();
    }

    public virtual Member Member
    {
        get { return _members.FirstOrDefault(); }
    }

    public virtual IEnumerable<Member> Members
    {
        get { return _members; }
    }

    public virtual void AddMember(Member member)
    {
        _members.Add(member);
    }

    public virtual void AddLastUsedVehicle(LastUsedVehicles lastUsedVehicles)
    {
        lastUsedVehicles.Vehicle = this;
        lastUsedVehicles.Member = this.Member;
        _lastUsedVehicles.Add(lastUsedVehicles);
    }

    public virtual void ClearLastUsedVehicles()
    {
        _lastUsedVehicles.Clear();
    }

    public virtual LastUsedVehicles LastUsedVehicle
    {
        get { return _lastUsedVehicles.FirstOrDefault(); }
    }

    public virtual IEnumerable<LastUsedVehicles> LastUsedVehicles
    {
        get { return _lastUsedVehicles; }
    }
}

LastUsedVehicles

    public class LastUsedVehicles : CompositeKeyBase
    {
        public virtual Vehicle Vehicle { get; set; }
        public virtual Member Member { get; set; }
        public virtual DateTime DateLastUsed { get; set; }

        public virtual bool Equals(LastUsedVehicles other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Vehicle, Vehicle) && Equals(other.Member, Member);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(LastUsedVehicles)) return false;
            return Equals((LastUsedVehicles)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Vehicle != null ? Vehicle.GetHashCode() : 0) * 397)
                    ^ Member.GetHashCode()
                    ^ DateLastUsed.GetHashCode();
            }
        }

    }

调试信息

为了更清楚,我还附加了一些SQL查询及其结果。

// Delete a member
delete from member m where account = 'X'; // FAIL - Child record found - The member has vehicles - So makes sense

// Delete all vehicles from a member from vehicles table
delete from vehicles mv where mv.vehicle_uid =
(select mv.vehicle_uid from member_vehicles mv where member_uid = (
select mm.member_uid from member mm where account = 'X')); //

// Delete all vehicles from a member from association table
delete from member_vehicles mv where member_uid = (
select mm.member_uid from member mm where account = 'X');

// Delete data on LASTUSED_VEHICLES - works
delete from LASTUSED_VEHICLES lv
where lv.vehicle_uid =
  (select mv.vehicle_uid from membervehicles mv where member_uid = (
          select mm.member_uid from member mm where account = 'X'));

LASTUSED_VEHICLES为空后,我可以删除成员。


很抱歉帖子的长度,但这个场景似乎很独特。感谢您的关注。

0 个答案:

没有答案