Nhibernate/Hibernate, lookup tables and object design

Posted by Simon G on Stack Overflow See other posts from Stack Overflow or by Simon G
Published on 2010-03-16T14:15:28Z Indexed on 2010/03/16 15:01 UTC
Read the original article Hit count: 419

Hi,

I've got two tables. Invoice with columns CustomerID, InvoiceDate, Value, InvoiceTypeID (CustomerID and InvoiceDate make up a composite key) and InvoiceType with InvoiceTypeID and InvoiceTypeName columns.

I know I can create my objects like:

public class Invoice
{
    public virtual int CustomerID { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Value { get; set; }
    public virtual InvoiceType InvoiceType { get; set; }
}

public class InvoiceType
{
    public virtual InvoiceTypeID { get; set; }
    public virtual InvoiceTypeName { get; set; }
}

So the generated sql would look something like:

SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID FROM Invoice WHERE CustomerID = x AND InvoiceDate = y
SELECT InvoiceTypeID, InvoiceTypeName FROM InvoiceType WHERE InvoiceTypeID = z

But rather that having two select queries executed to retrieve the data I would rather have one. I would also like to avoid using child object for simple lookup lists. So my object would look something like:

public class Invoice
{
    public virtual int CustomerID { get; set; }
    public virtual DateTime InvoiceDate { get; set; }
    public virtual decimal Value { get; set; }
    public virtual InvoiceTypeID { get; set; }
    public virtual InvoiceTypeName { get; set; }
}

And my sql would look something like:

SELECT CustomerID, InvoiceDate, Value, InvoiceTypeID 
FROM Invoice INNER JOIN InvoiceType ON Invoice.InvoiceTypeID = InvoiceType.InvoiceTypeID
WHERE CustomerID = x AND InvoiceDate = y

My question is how do I create the mapping for this?

I've tried using join but this tried to join using CustomerID and InvoiceDate, am I missing something obvious?

Thanks

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping