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.Create开发者_运维问答dDate", 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.
Are you sure it is a cartesian product, or you just receive "duplicated" rows in the retuned list ?
If so, before .List()
, try to call .SetResultTransformer(Transformers.DistinctRootEntity)
, because by eager fetching the associated collection you receive a list containing duplicates of the same entity.
You can have a single FetchMode.Join before you start creating cartesian products. What if you switch others to FetchMode.Select or something else?
精彩评论