how to keep reference to last added element in collection?

21 views
Skip to first unread message

Bruno Sanches

unread,
Aug 10, 2012, 12:52:10 PM8/10/12
to nhu...@googlegroups.com

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

Fran Knebels

unread,
Aug 10, 2012, 1:01:07 PM8/10/12
to nhu...@googlegroups.com
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.


--
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.

Bruno Sanches

unread,
Aug 10, 2012, 1:25:32 PM8/10/12
to nhu...@googlegroups.com
Hello,

because we do not want to force it to be always the last, we want to keep a "moving pointer" to the current one.

Thank you
Bruno

Fran Knebels

unread,
Aug 10, 2012, 1:34:54 PM8/10/12
to nhu...@googlegroups.com
right.

if you are populating the the list on load and ordering it ascending like your map says.  then the current one is always the last in the list.

if you add any new ones during your session, they are newer than the ones in the list and the last one is still the current one.

maybe i'm not understanding your problem.  what, from a business sense, are you trying to achieve?

To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/59TqDxRkXq8J.

Bruno Sanches

unread,
Aug 10, 2012, 2:29:08 PM8/10/12
to nhu...@googlegroups.com
This class Vehicle represents a railroad vehicle, each vehicle has a model, that describe its features. Specially for passenger cars, the model can be quite complex, with seating layout, seat properties, etc.

So each vehicle has a reference to its model and models can be shared.

Because each vehicle can be changed during its lifetime, its model may change. We decided to simple lock the models after they start being used, so for a vehicle to change, it will need a new model. 

To complicate it a bit more, it was decided that we need to keep a history of all changes to a vehicle, hence now we have this VehicleModelItem, to create a Many to Many relationship. 

Basically, each Vehiche can have many models and each model can have many vehicles and also, each vehicle tracks the current active model.

I see that ordering by the date I would always have the latest model or perhaps I can have a sequence number on each VehicleModelItem, but right now I do not feel confident to rely on date for this. 

Any sugestions are welcome!

Thank you
Bruno Sanches
Reply all
Reply to author
Forward
0 new messages