0
votes

I am new with Fluent NHibernate and I have a problem with a particular HasMany relationship.

I have three objects: User, Company and CompanyAddress

User references Company correctly and Company would reference CompanyAddress (but this fails).

Company mapping is composed like following:

public class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Schema("company");
        Table("LIST");

        this.CompositeId(x => x.key)
            .KeyProperty(x => x.applicationId, "APPLICATION_ID")
            .KeyProperty(x => x.id, "ID")
            .KeyProperty(x => x.userId, "USER_ID");

        this.Map(x => x.vat, "VAT");

        this.HasMany(x => x.addresses)
            .Not.LazyLoad()
            .Cascade.None();

        this.References(x => x.user)
            .Class<User>()
            .Columns("APPLICATION_ID", "USER_ID")
            .Insert()
            .Update()
            .Fetch.Select()
            .Not.LazyLoad()
            .Cascade.None();
    }
}

and CompanyAddress is:

public class CompanyAddressMap : ClassMap<CompanyAddress>
{
    public CompanyAddressMap()
    {
        Schema("[address]");
        Table("COMPANY_LIST");

        this.CompositeId(x => x.key)
            .KeyProperty(x => x.applicationId, "APPLICATION_ID")
            .KeyProperty(x => x.id, "ID")
            .KeyProperty(x => x.entityId, "COMPANY_ID");

        this.Map(x => x.country, "COUNTRY");
        this.Map(x => x.province, "PROVINCE");
        this.Map(x => x.region, "REGION");
        this.Map(x => x.city, "CITY");
        this.Map(x => x.address, "[ADDRESS]");
        this.Map(x => x.description, "DESCRIPTION");

        this.References(x => x.company)
            .Class<Company>()
            .Columns("APPLICATION_ID", "COMPANY_ID")
            .Insert()
            .Update()
            .Fetch.Select()
            .Not.LazyLoad()
            .Cascade.None();
    }
}

but I receive this error:

Foreign key (FK8DE1BC098F20893F:COMPANY_LIST [APPLICATION_ID, COMPANY_ID])) must have same number of columns as the referenced primary key (LIST [APPLICATION_ID, ID, USER_ID])

I need to reference AddressCompany with non primary key because Company is already referenced by User and so my question is:

How can I reference this foreign key using Fluent NHibernate without modifying the real foreign key defined into the database?

[EDIT]

i need to reference AddressCompany with non the primary key 'cause Company is already referenced by User and so my question is: how can i reference this foreign key using fluent nhibernate without modifying the real foreign key defined into the database?

i don't want to add USER_ID field to the [address].[COMPANY_LIST] table and i don't want to add userId field to AddressCompany class... 'cause is a useless field... the relationships are:

user.LIST
applicationId (pk)
id (pk)

company.LIST
applicationId (pk) (fk -> user.LIST(applicationId))
id (pk)
userId (pk) (fk -> user.LIST(id))

address.COMPANY_LIST
applicationId (pk) (fk -> company.LIST(applicationId))
id (pk)
companyId (pk) (fk -> company.LIST(id))

why nhibernate wants force me to reference only the exact primary key?
could i reference a subset of the primary key of Company class?

thanks so much...

1

1 Answers

0
votes

The error seems pretty self evident from your mapping and error message

Foreign key (FK8DE1BC098F20893F:COMPANY_LIST [APPLICATION_ID, COMPANY_ID])) must have same number of columns as the referenced primary key (LIST [APPLICATION_ID, ID, USER_ID])

You mappings for CompanyMap maps your primary key for as three columns

    this.CompositeId(x => x.key)
        .KeyProperty(x => x.applicationId, "APPLICATION_ID")
        .KeyProperty(x => x.id, "ID")
        .KeyProperty(x => x.userId, "USER_ID");

but your references to Company in CompanyAddressMap only maps back to Company with 2 columns

    this.References(x => x.company)
        .Class<Company>()
        .Columns("APPLICATION_ID", "COMPANY_ID")
        .Insert()
        .Update()
        .Fetch.Select()
        .Not.LazyLoad()
        .Cascade.None();

Add the ID column to your columns listing.