views:

232

answers:

1

Hello,

I have two tables that I want to map to one class that will looks like:

CUSTOMER_INFO_CLASS.cs
----------------------
Id (CUSTOMER table) 
CustomerName (CUSTOMER table)
CustomerTypeDesc (CUSTOMER_TYPE table)

I tried to do it with join, as follows:

Table("CUSTOMER");

Id(x => x.ID).Length(10).Column("CustomerId");
Map(x => x.CustomerName);

Join("CUSTOMER_TYPE", m =>
    {
    m.Optional();
    m.Map(x => x.CustomerTypeDesc);
    m.KeyColumn("CustomerType");
    });

The problem is that the field with whom I'm trying to link the two tables is not a primary key in any of them. (And by default the join done by the field that defined as ID) So I found that for the CUSTOMER_TYPE table I can define the field by “KeyColumn”.
How can I define that the related column in the CUSTOMER table will be CustomerTypeCode and not CustomerId? (if I can at all)

At the end the sql query should looks like:

Select Id, CustomerName, CustomerAddress, CustomerTypeDesc
From CUSTOMER t1
  Left join CUSTOMER_TYPE t2
    On t1.CustomerTypeCode = t2.CustomerType
A: 

If the Customer table maps the CustomerType member to the primary key of the CustomerType table, then Hibernate should do the join automatically for you.

Is there a reason why the CustomerType is not linked by a normal foreign key reference?

Jon Seigel
The CustomerTypeCode is not a primary key of CUSTOMER and CustomerType is not a primary key of CUSTOMER_TYPE, and from this our problem begins.I dont know why the CustomerType is not linked by a normal foreign key,the DB was built some time ago. And it's not an option change it, because another system uses it...
Olga
Another thing I noticed is that when using join, the fluint doing the connection like this: JOIN field that defined as ID from first table = field that i defining in "KeyColumn" from second table. But i don't want to use ID column for join, I want other column from first table, how can i tell it to nhibernate...
Olga

related questions