I have to model this relationship in NHibernate (simplified the code a bit to stay on-topic) - an employee can be an accountmanager (so, it's optional):
table Employee (Id, Number, Name) table EmployeeIsAccountManager (Number, MaxAllowedDiscount)
instead of having an Id foreign key in table EmployeeIsAccountManager pointing to table Employee, I have a Number column in the Employee table which points to the Number column in table EmployeeIsAccountManager.
How do I map this in NHibernate? I've tried using the foreign generator on the EmployeeIsAccountManager class mapping, but if I use Number as foreign generated value, it maps to the ID of Employee, which is Id instead of Number. I modeled my class to use composition:
public class Employee
{
public virtual int Id { get; set; }
public virtual short Number {get; set; }
public virtual string Name {get; set; }
public virtual AccountManager AccountManager { get; set; }
}
public class AccountManager
{
public virtual short Number { get; set; } /*needed because of ID only?*/
public virtual decimal MaxAllowedDiscount { get; set }
}
I've tried a lot (one-to-one, many-to-one, foreign generator) but I can't figure out if this can be done with NHibernate. btw: I can change the classes, mappings, etc but I CANNOT change the table structure because of it's brownfield status (old application with 2+ million lines of code, nearly 1000 forms).
Any help is appreciated, thanks! Ted