Search code examples
nhibernatenhibernate-criteriacartesian-product

NHibernate FetchMode Cartesian Product


In my object graph, VendorServiceRateChange has a lazy loaded property IList<VendorService> VendorServiceList and the VendorService has a lazy loaded property IList<ClientService>.

When I run the following code I get a Cartesian product between my VendorServiceList and my ClientServiceList.

queueList = session
    .CreateCriteria<VendorRateChange>()
    .Add(Restrictions.IsNull("ProcessedDate"))
    .Add(Restrictions.Le("EffectiveDate", DateTime.Now))
    .SetFetchMode("VendorServiceList", FetchMode.Join)
    .SetFetchMode("VendorServiceList.Vendor", FetchMode.Join)
    .SetFetchMode("VendorServiceList.CityService", FetchMode.Join)
    .SetFetchMode("VendorServiceList.ClientServices", FetchMode.Join)
    .SetResultTransformer(new DistinctRootEntityResultTransformer())
    .AddOrder(new Order("Audit.CreatedDate", true))
    .List<VendorRateChange>();

Is there a way to structure this query using Criteria or DetachedCriteria that would not result in a Cartesian product as my VendorServiceList? I do not want to have to resort to commenting out the fetch on "VendorServiceList.ClientServices" and looping through with an Initialize call after the initial query has come back.

Thanks in advance.


Solution

  • You can have a single FetchMode.Join before you start creating cartesian products. What if you switch others to FetchMode.Select or something else?