3

I'm having a problem with the following scenario.

My class structure is as follows:

public class Owner
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Vehicle> Vehicles { get; set; }
}

public abstract class Vehicle
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
}

public abstract class PoweredVehicle : Vehicle
{
    public virtual string EngineType { get; set; }
}

public class Car : PoweredVehicle
{
    public virtual int Doors { get; set; }
}

public class Truck : PoweredVehicle
{
    public virtual long MaxLoad { get; set; }
}

public class Bicycle : Vehicle
{
    public virtual int FrameSize { get; set; }
}

Fluent mappings:

public class OwnerMap : ClassMap<Owner>
{
    public OwnerMap()
    {
        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.Name);
        HasMany(x => x.Vehicles);
    }
}

public class VehicleMap : ClassMap<Vehicle>
{
    public VehicleMap()
    {
        Id(x => x.Id).GeneratedBy.HiLo("10");
        Map(x => x.Name);
        UseUnionSubclassForInheritanceMapping();
    }
}

public class PoweredVehicleMap : SubclassMap<PoweredVehicle>
{
    public PoweredVehicleMap()
    {
        Map(x => x.EngineType);
        Abstract();
    }
}

public class CarMap : SubclassMap<Car>
{
    public CarMap()
    {
        Map(x => x.Doors);
    }
}

public class TruckMap : SubclassMap<Truck>
{
    public TruckMap()
    {
        Map(x => x.MaxLoad);
    }
}

public class BicycleMap : SubclassMap<Bicycle>
{
    public BicycleMap()
    {
        Map(x => x.FrameSize);
    }
}

I insert a Car and a Bicycle. When I try to insert an Owner with a list of Vehicle objects (with a Car and a Bicycle), I get the following error:

Exception: NHibernate.Exceptions.GenericADOException: could not insert collection: [NHibernateTest.Owner.Vehicles#8ace95bc-ad80-46d7-94c7-a11f012b67c6][SQL: UPDATE "Vehicle" SET Owner_id = @p0 WHERE Id = @p1] ---> System.Data.SQLite.SQLiteException: SQLite error

Since I setup table per concrete class, why is NHibernate trying to update a non-existing table, which is representing the base class? Is this type of mapping not supported for this scenario?

Also, when I change from HasMany to HasManyToMany, this works fine.

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Mas
  • 4,546
  • 5
  • 39
  • 56

1 Answers1

2

In this case the only choice is Inverse() mapping. This means that the concrete Vehicle (Car, Bicycle) must care about the persistence of the relationship.

To enable this, extend the Vehicle class with new property:

public abstract class Vehicle
{ 
  ..
  // new owner independent navigation property
  public virtual Guid OwnerId { get; set; } 
}

and extend mapping of the Vehicle

public VehicleMap()
{  
  ..
  Map(x => x.OwnerId).Column("Owner_id);
}

and finally invert persistence responsibility. Not the owner, but the collection item will care about correct Owner_id column changes (when concrete Vehicle insert/update is invoked).

(more about inverse: https://stackoverflow.com/a/1454445/1679310)

public OwnerMap()
{
  ..
  HasMany(x => x.Vehicles)
    .Inverse()
    .Cascade.All();
}

When Vehicle is added into Owner's collection, its OwnerId must be also assigned:

owner.Vehicles.Add(car);
car.OwnerId = owner.Id;
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 1
    Thanks, that works great. It also works by adding the Owner object to the Vehicle object, and use a References() in VehicleMap. Then NHibernate automatically creates the Owner_id column in each of the concrete class tables. – Mas Dec 07 '12 at 08:40