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--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/59TqDxRkXq8J.