简体   繁体   中英

Force fluent-nhibernate join on correct column?

When using these entities:

[AS400Entity]
public class AuthItemEntity
{
    public virtual decimal Upc { get; set; }
    public virtual string VendorItemCode { get; set; }
    public virtual int Vendor { get; set; }
    public virtual int ProductVendorNumber { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime AddDate { get; set; }
    public virtual DateTime VendorDiscontinueDate { get; set; }

    public virtual VendorMasterEntity VendorMaster { get; set; }
}

[AS400Entity]
public class VendorMasterEntity
{
    public virtual int CompanyNumber { get; set; }
    public virtual int Vendor { get; set; }
    public virtual CompNamesEntity CompNames { get; set; }
    public virtual IList<AuthItemEntity> AuthItems { get; set; }
}

[AS400Entity]
public class CompNamesEntity
{
    public virtual int CompanyNumber { get; set; }
    public virtual string CompanyName { get; set; }
    public virtual IList<VendorMasterEntity> VendorMasts { get; set; }
}

And this mapping:

public class AuthItemEntityMapping : ClassMap<AuthItemEntity>
{
    public AuthItemEntityMapping()
    {
        Table("AUTHITEM");
        Id(entity => entity.Vendor, "ITEM_VENDOR");
        Map(entity => entity.ProductVendorNumber, "PRODUCT_VENDOR");
        Map(model => model.Upc, "ITEM_UPC");
        Map(model => model.VendorItemCode, "VENDOR_ITEM_CODE");
        Map(model => model.Vendor, "ITEM_VENDOR");
        Map(model => model.Description, "ITEM_DESCRIPTION");
        Map(model => model.AddDate, "ADD_DATE").CustomType("Date");
        Map(model => model.VendorDiscontinueDate, "VENDOR_DISCONTINUE_DATE").CustomType("Date");

        HasOne(entity => entity.VendorMaster).Fetch.Join().ForeignKey("ITEM_VENDOR");
    }
}

public class VendorMasterEntityMap : ClassMap<VendorMasterEntity>
{
    public VendorMasterEntityMap()
    {
        Table("VENDMAST");

        Id(entity => entity.Vendor, "ITEM_VENDOR");
        Id(entity => entity.CompanyNumber, "COMPANY_NUMBER");

        HasOne(entity => entity.CompNames).Fetch.Join().ForeignKey("COMPANY_NUMBER");

        HasMany(entity => entity.AuthItems).Cascade.All();
    }
}

public class CompNamesEntityMap : ClassMap<CompNamesEntity>
{
    public CompNamesEntityMap()
    {
        Table("COMPNAMES");
        Id(entity => entity.CompanyNumber, "COMPANY_NUMBER");
        Map(entity => entity.CompanyName, "COMPANY_NAME");

        HasMany(entity => entity.VendorMasts).Cascade.All();
    }
}

I get this SQL:

SELECT this_.ITEM_VENDOR as ITEM1_3_2_, this_.PRODUCT_VENDOR as PRODUCT2_3_2_, this_.ITEM_UPC as ITEM3_3_2_, this_.VENDOR_ITEM_CODE as VENDOR4_3_2_, this_.ITEM_DESCRIPTION as ITEM5_3_2_, this_.ADD_DATE as ADD6_3_2_, this_.VENDOR_DISCONTINUE_DATE as VENDOR7_3_2_, vendormast2_.COMPANY_NUMBER as COMPANY1_5_0_, compnamese3_.COMPANY_NUMBER as COMPANY1_4_1_, compnamese3_.COMPANY_NAME as COMPANY2_4_1_ 
FROM AUTHITEM this_ 
    left outer join VENDMAST vendormast2_ on this_.ITEM_VENDOR=vendormast2_.COMPANY_NUMBER <-- should be item_vendor
    left outer join COMPNAMES compnamese3_ on vendormast2_.COMPANY_NUMBER=compnamese3_.COMPANY_NUMBER WHERE this_.ITEM_UPC = ? and this_.VENDOR_DISCONTINUE_DATE = ?

Which is just a property reference away from being right.

Please note: I'm using HasOne() everywhere because I can't have this lazy loading, it must load at the time of querying.

As of December 2014, you still cannot fix this issue:

https://github.com/jagregory/fluent-nhibernate/issues/272

It makes sense because it's an ORM and we were trying to use it on a database without properly enforced relationships (the keys are missing but the data behaves this way).

This issue has forced my company to drop NHibernate altogether.

I hope that this answer saves someone else from making the same mistake and wasting their time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM