0

I am trying to build the following association in NHibernate (the base Entity class contains an Id property, it and the attributes are from S#arp Architecture)

public class Terminal : Entity {
    public virtual string Name { get; set; }
}
public class Order : Entity {
    [NotNull]
    public virtual Terminal Terminal { get; set; }
}

Where the data on order is stored in ORDERS and data on terminals in stored in the TERMINALS tables. The problem is that there is no direct link between the two tables. If I had an id for ORDERS.ORDER_ID I would fetch the corresponding TERMINALS row with the following SQL.

select t.* 
from ORDERS o 
     inner join cust_prod cp on (o.CUST_PROD_ID = cp.CUST_PROD_ID)
     inner join customer_terminal ct on (cp.CUSTOMER_TERMINAL_ID = ct.CUSTOMER_TERMINAL_ID)
     inner join terminal t on (ct.TERMINAL_ID = t.TERMINAL_ID)
where o.ORDER_ID = :p_order_id

How do I map this in NHibernate? Preferably how do I map this in Fluent NHibernate but I think I could figure it out from the XML. Also, given that the legacy database I'm working with is full of chains like this are there any best practices suggestions?

5
  • What are the customer and order classes?
    – Paco
    Jun 15, 2009 at 21:49
  • I don't know what you mean. I gave a complete description of the order class and while I have a Customer class its existence is orthogonal to the problem at hand. Jun 16, 2009 at 0:37
  • I dont't see what you want to do with inner join cust_prod cp on (o.CUST_PROD_ID = cp.CUST_PROD_ID) and customer_terminal ct on (cp.CUSTOMER_TERMINAL_ID = ct.CUSTOMER_TERMINAL_ID)
    – Paco
    Jun 16, 2009 at 11:36
  • I am trying to have an order reference a terminal. The database is poorly normalized however and given an Order.id substituted for :p_order_id the query is how I would get information on the terminal. Jun 16, 2009 at 13:42
  • it's not possible to query nhibernate on tables that are not mapped. you can create a normal sql query or normalize the db anyway i guess
    – Paco
    Jun 16, 2009 at 15:48

1 Answer 1

1

I'm pretty sure your options would be to:

1) Map against raw SQL (look at the NHibernate doc definition for sql-query: nh docs).. The section on mapping updates / inserts is right after.

2) Or, introduce at least entities in the middle to handle holding the relationships in cust_prod and customer_terminal. If those tables are using composite keys for the PKs then you will want to look at how to map composite-id and key-many-to-one.

In the second option, you'd then map either a one-to-one (or bi-directional many-one) on the join entities.

3) I haven't looked at stored procs in forever, but I believe NH 2.0+ introduced better sproc support. That might be a viable third option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.