How can I map to a field that is joined in via one of three possible tables

Posted by Mongus Pong on Stack Overflow See other posts from Stack Overflow or by Mongus Pong
Published on 2012-10-31T16:58:50Z Indexed on 2012/10/31 17:00 UTC
Read the original article Hit count: 214

Filed under:
|
|

I have this object :

public class Ledger
{
  public virtual Person Client { get; set; }
  // ....
}

The Ledger table joins to the Person table via one of three possible tables : Bill, Receipt or Payment.

So we have the following tables :

Ledger LedgerID PK

Bill BillID PK, LedgerID, ClientID

Receipt ReceiptID PK, LedgerID, ClientID

Payment PaymentID PK, LedgerID, ClientID

If it was just the one table, I could map this as :

Join ( "Bill", x =>
{
    x.ManyToOne ( ledger => ledger.Client, mapping => mapping.Column ( "ClientID" ) );
    x.Key ( l => l.Column ( "LedgerID" ) );
} );

This won't work for three tables. For a start the Join performs an inner join. Since there will only ever be one of Bill, Receipt or Payment - an inner join on these tables will always return zero rows.

Then it would need to know to do a Coalesce on the ClientID of each of these tables to know the ClientID to grab the data from.

Is there a way to do this? Or am I asking too much of the mappings here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate