views:

36

answers:

1

So, I'm having a problem mapping in fluent nhibernate. I want to use a join mapping to flatten an intermediate table: Here's my structure:

[Vehicle]
VehicleId
...

[DTVehicleValueRange]
VehicleId
DTVehicleValueRangeId
AverageValue
...

[DTValueRange]
DTVehicleValueRangeId
RangeMin
RangeMax
RangeValue

Note that DTValueRange does not have a VehicleID. I want to flatten DTVehicleValueRange into my Vehicle class. Tgis works fine for AverageValue, since it's just a plain value, but I can't seem to get a ValueRange collection to map correctly.

    public VehicleMap()
    {
        Id(x => x.Id, "VehicleId");
        Join("DTVehicleValueRange", x =>
        {
            x.Optional();
            x.KeyColumn("VehicleId");
            x.Map(y => y.AverageValue).ReadOnly();
            x.HasMany(y => y.ValueRanges).KeyColumn("DTVehicleValueRangeId"); // This Guy
        });
    }

The HasMany mapping doesn't seem to do anything if it's inside the Join. If it's outside the Join and I specify the table, it maps, but nhibernate tries to use the VehicleID, not the DTVehicleValueRangeId.

What am I doing wrong?

A: 

Can you explain the average value column in the DTVehicleValueRange table? Isn't this a calculated value (i.e. no need to persist it)?

It looks like you have a many-to-many relationship between Vehicle and DTValueRange, which of course would not be mapped with a join, rather with a HasManyToMany call.

Jay
The DTVehicleValueRange is extracted from another data source; We don't have access to the source values at run time, so we do persist the average. Since the base values are updated quarterly, there's no real time requirement. The Vehicle to DTValueRange is mapped as a many-to-many, but in reality, there's always a one-to-one relationship. (I know that's not recommended.)
Sean McMillan