How we can fetch more than one grandchild collections

175 views Asked by At

I have this table structure:

     ReferralSource - main table
      -Phone - join table Rs as one-to-one
         - Carrier - child table for Phone
         - Type    - child table for Phone

I want to get it by Linq query:

Session.Query<ReferralSource>()                
            .Fetch(x => x.Phone)
            .ThenFetch(x => x.Type)
            .Fetch(x => x.Phone)
            .ThenFetch(x => x.Carrier);

And this query get this SQL code:

  select referralso0_.Id,                        
               referralso0_.FirstName,                   
               phonetype4_.TypeName
               carrier6_.Name                  
        from   REFERRALSOURCES referralso0_                  
               left outer join PHONES phone3_
                 on referralso0_.PhoneId = phone3_.Id
               left outer join PHONETYPES phonetype4_
                 on phone3_.TypeId = phonetype4_.Id 
           - duplicated join started
           left outer join PHONES phone5_
             on referralso0_.PhoneId = phone5_.Id
           left outer join CARRIERS carrier6_
             on phone5_.CarrierId = carrier6_.Id - duplicated join finished

How can I remove duplicated left join using fetch?

1

There are 1 answers

1
Firo On

Maybe if you map Phone as a Component instead an entity

public ReferralSourceMap()
{
    Join("Phones", join =>
    {
        join.KeyColumn("...");
        join.Component(x => x.Phone, c => { ... });
    });
}

Edit: unfortunatly this will not work with LINQ2NHibernate only with Criteria and QueryOver. Should i delete the answer?