I'm not sure why you are trying to do this in the mapping.

If you just want the last vehicle added why not just map the CurrentVehicle
property to something like this

VehicleModel {
get
{
return mVehicleModels[mVehicleModels.Count - 1];
}

Since you are populating this list already and you only need a reference to
the last one in the list.


On Fri, Aug 10, 2012 at 12:52 PM, Bruno Sanches <[email protected]> wrote:

> I have a collection of items in one object and I need to keep reference to
> the last added item to the collection, so I am doing like this:
>
> 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();
>
>
>         }
>
>
>     }
>
>  And the Vehicle class looks like this:
>
>   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();
>
>
>         }
>
>  The problem is, if I create a new vehicle, like:
>
> Vehicle vehicle = new Vehicle();
>
>
> vehicle.Model = anyExitingVehicleModel;
>
>
> session.save(vehicle);
>
>  I get an "null or transient object set to not null property", in this
> case for the property Vehicle in the VehicleModelItem.
>
> The problem is related to the mCurrenveVehicleModel property in vehicle,
> if I removed it, everything works find, but I could not track the last
> added item (at least not by this way).
>
> Is there anyway that I can make this work without needing to call save for
> both the VehicleModelItem and vehicle?
>
> I tried several combinations of Cascade on those collections, but without
> success.
>
> Thank you
> Bruno Sanches
> ========================
> http://www.pontov.com.br
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to