views:

127

answers:

1

I have been reading Nhibernate in Action, but the section on mapping polymorphic collections is a little too short on how to do this.

I have the following code

[Class]
[Discriminator(Column="MachineType",TypeType=typeof(string))]
public abstract class Machine
{
  [Property]
  public string Name{get;set;}
}

[Subclass(DiscriminatorValue="Heavy",ExtendsType=typeof(Machine))]
public class HeavyMachine : Machine
{
  [Property]
  public int Weight { get; set; }
}

[Subclass(DiscriminatorValue="Fast",ExtendsType=typeof(Machine))]
public class FastMachine : Machine
{
  [Property]
  public float Speed { get; set; }
}

[Class]
public class Module
{
    List<Machine> machines = new List<Machine>();

    [Bag(Name = "Machines", Cascade = "all", Lazy = false, Inverse=true)]
    [Key(1, Column = "Machine")]
    [OneToMany(2, ClassType = typeof(Machine))]
    public IList<Machine> Machines
    {
      get
      {
        return machines.AsReadOnly();
      }
      private set
      {
        machines = value.ToList();
  }
    }
}

With the code above I am not getting any errors, but the Collection of machines in Module remains empty after retreiving my objects from the database. The mapping of the Machine (and it's subclasses) does seem ok, because a property of type Machine is correctly returned.

What Nhibernate.Mapping.Attributes do I need to map my collection of abstract classes?

thx in advance!

+1  A: 

Ok I found the solution. After removing the "Inverse=true" tag from my IList mapping it worked.

sjors miltenburg