Eager loading child collection with NHibernate

前端 未结 3 719
粉色の甜心
粉色の甜心 2020-12-17 09:03

I want to load root entities and eager load all it\'s child collection and aggregate members.

Have been trying to use the SetFetchMode in FluentNHiberna

3条回答
  •  星月不相逢
    2020-12-17 09:13

    Found a solution but it isn't pretty. First I go and find all the Invoice IDs, then I use them in the multiquery and then at the end filtering the results through a HashedSet. Because of the large number of items sometimes i couldn't use the normalt Restriction.In and was forced to send it as a string.

    Any suggested tweaks?

    var criteria = Session.CreateInvoiceBaseCriteria(query, archived)
        .SetProjection(Projections.Id());
    
    var invoiceIds = criteria.List();
    if (invoiceIds.Count > 0)
    {
        var joinedIds = JoinIDs(criteria.List()); // To many ids to send them as parameters.
    
        var sql1 = string.Format("from Invoice i inner join fetch i.States where i.InvoiceID in ({0}) order by i.{1} {2}", joinedIds, query.Order, query.OrderType.ToString());
        var sql2 = string.Format("from Invoice i inner join fetch i.AttestationRequests where i.InvoiceID in ({0})", joinedIds);
        var sql3 = string.Format("from Invoice i inner join fetch i.Attestations where i.InvoiceID in ({0})", joinedIds);
    
        var invoiceQuery = Session.CreateMultiQuery()
            .Add(sql1)
            .Add(sql2)
            .Add(sql3);
    
        var result = invoiceQuery.List()[0];
    
        return new UniqueFilter((ICollection)result);
    }
    
    return new List();
    

提交回复
热议问题