NHibernate:如何保持对集合中最后添加的元素的引用?

时间:2012-08-10 14:42:12

标签: c# nhibernate fluent-nhibernate

我在一个对象中有一个项目集合,我需要继续引用最后添加的项目到集合中,所以我这样做:

public class VehicleMap : BaseUniqueNamedEntityMap<Vehicle>
    {
        public VehicleMap():
            base("vehicle_id", 40)
        {
            Not.LazyLoad();                

            //The main collection of items
            HasMany(x => x.mVehicleModels)                                        
                .OrderBy("date asc")
                .Cascade.AllDeleteOrphan()                    
                .LazyLoad()
                .KeyColumn("vehicle_id");

            //Always updated to the last added item
            References(x => x.mCurrentModel)
                .Column("current_vehicle_model_relation_id")
                .Cascade.None()
                .Not.Nullable();
        }
    }

Vehicle类看起来像这样:

  public class Vehicle : BaseUniqueNamedEntity
{        
    private IList<VehicleModelItem> mVehicleModels = new List<VehicleModelItem>();

    [NotNull()]
    private VehicleModelItem mCurrentModel;

    public VehicleModel Model
    {
        get { return mCurrentModel.VehicleModel; }

        set 
        {
            //Check null before checking if values change, so we catch exceptions on constructor also
            if (value == null)
                throw new ArgumentNullException("Vehicle model cannot be null");                

                //When model is set, we update current model and insert it on list
                mCurrentModel = new VehicleModelItem(this, value);
                mVehicleModels.Add(mCurrentModel);
                value.AddVehicleModelItem(mCurrentModel);                    
        }
    }          
}

VehicleModelItem mapping:

public VehicleModelItemMap() :
            base("vehicle_model_relation_id")
        {
            Not.LazyLoad();                

            References(x => x.mVehicleModel)
                .Column("vehicle_model_id")
                .Cascade.None()
                .LazyLoad(Laziness.Proxy)
                .Not.Nullable();

            Map(x => x.mDate)
                .Column("date")
                .Not.Nullable();
        }

问题是,如果我创造一种新车,例如:

Vehicle vehicle = new Vehicle();
vehicle.Model = anyExitingVehicleModel;

session.save(vehicle);

我得到“null或者瞬态对象设置为非null属性”,在本例中为VehicleModelItem中的属性Vehicle。

问题与车辆中的mCurrenveVehicleModel属性有关,如果我将其删除,一切正常,但我无法追踪最后添加的项目(至少不是这样)。

无论如何我可以在不需要为VehicleModelItem和车辆调用保存的情况下完成这项工作吗?

我在这些系列上尝试了几种Cascade组合,但没有成功。

谢谢

1 个答案:

答案 0 :(得分:0)

尝试将您的VehicleMap更改为:

public class VehicleMap : BaseUniqueNamedEntityMap<Vehicle>
{
    public VehicleMap():
        base("vehicle_id", 40)
    {
        Not.LazyLoad();                

        //The main collection of items
        HasMany(x => x.mVehicleModels)                                        
            .OrderBy("date asc")
            .Cascade.AllDeleteOrphan()                    
            .LazyLoad()
            .KeyColumn("vehicle_id")
            .Inverse();

        //Always updated to the last added item
        References(x => x.mCurrentModel)
            .Column("current_vehicle_model_relation_id")
            .Not.Nullable()
            .Not.Insert();
    }
}

这与我的做法类似,只是我手动设置'引用'部分而不是将其包含在setter中。唯一的缺点是,如果将'Inverse'设置为true,则必须在VehicleModelItem上手动指定父级。由于构造函数正在传入该引用,我假设您已经在执行该操作。