0
votes

I've been trying for ages to find an example (because I can't get it to work myself) of the correct mapping for a one-to-many relationship on an abstract class of a table-per-subclass implementation, in fluent nHibernate.

An example below: I'm looking to map the list of Fines on the Debt abstract base class to the Fine class.

If anyone knows of any tutorial or example they've come across before please let me know.

public abstract class Entity
{
    public int Id { get; set; }
}

public abstract class Debt : Entity
{        
    public decimal Balance { get; set; }

    public IList<Fine> Fines { get; set; }

    public Debt()
    {
        Fines = new List<Fine>();
    }

}

public class CarLoan : Debt
{

}

public class CreditCard : Debt
{

}

public class LoanApplication : Entity
{
    public IList<Debt> ExistingDebts { get; set; }

    public LoanApplication()
    {
        ExistingDebts = new List<Debt>();
    }
}

public class Fine
{
    public Int64 Cash { get; set; }
}
1

1 Answers

0
votes

Can you tell us where exactly you're having difficulty? What have you tried?

Obviously, you'll need to declare all of your members as virtual (I assume this was an oversight in the example).

Basically, though, it would look like this:

public DebtMap : ClassMap<Debt>
{
    public DebtMap()
    {
        Id(x => x.Id);
        HasMany(x => x.Fines);
    }
}

public FineMap : ClassMap<Fine>
{
    public FineMap()
    {
        Id(x => x.Id);
        // map other members
    }
}

public CarLoanMap : SubclassMap<CarLoan> { }
public CreditCardMap : SubclassMap<CreditCard> { }