1

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

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 映射:

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);

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

问题与车辆中的 mCurrenveVehicleModel 属性有关,如果我删除它,一切正常,但我无法跟踪最后添加的项目(至少不是通过这种方式)。

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

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

谢谢

4

1 回答 1

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 上手动指定父级。由于构造函数传入了该引用,我假设您已经这样做了。

于 2012-08-10T16:52:28.870 回答