from v in session.Query<Vehicle>() where v.Carrier ==
session.Load<Carrier>(1) select v

On Wed, Jun 23, 2010 at 8:16 AM, ben <[email protected]> wrote:

> I've got a one to many association between a Carrier and Vehicle. In
> my database my Vehicle table has a foreign key CarrierId.
>
> The most efficient way to query vehicles by carrier would be:
>
> select ... from Vehicles where Vehicles.CarrierId == 1
>
> However, if I use QBC:
>
>                var fromDb = session.CreateCriteria<Vehicle>()
>                        .CreateCriteria("Carrier",
> global::NHibernate.SqlCommand.JoinType.InnerJoin)
>                        .Add(Expression.Eq("Id", 1))
>                    .AddOrder(new Order("BusinessRef", true))
>                .List<Vehicle>();
>
> The result is:
>
> NHibernate: SELECT this_.Id as Id1_1_, this_.VehicleType as
> VehicleT2_1_1_, this_.BusinessRef as Business3_1_1_, this_.CarrierId
> as CarrierId1_1_, carrier1_.Id as Id2_0_, carrier1_.Name as Name2_0_,
> carrier1_.BusinessRef as Business3_2_0_ FROM Vehicles this_ inner join
> Carriers carrier1_ on this_.CarrierId=carrier1_.Id WHERE carrier1_.Id
> = @p0 ORDER BY carrier1_.BusinessRef asc;@p0 = 1
>
> If I use NHibernate.Linq:
>
>                var fromDb2 = session.Linq<Vehicle>()
>                    .Where(x => x.Carrier.Id == 1)
>                    .OrderBy(x => x.BusinessRef)
>                    .ToList();
>
> The result is similar but always seems to do an outer join (how can I
> force it to be left inner?):
>
> NHibernate: SELECT this_.Id as Id1_1_, this_.VehicleType as
> VehicleT2_1_1_, this_.BusinessRef as Business3_1_1_, this_.CarrierId
> as CarrierId1_1_, carrier1_.Id as Id2_0_, carrier1_.Name as Name2_0_,
> carrier1_.BusinessRef as Business3_2_0_ FROM Vehicles this_ left outer
> join Carriers carrier1_ on this_.CarrierId=carrier1_.Id WHERE
> carrier1_.Id = @p0 ORDER BY this_.BusinessRef asc;@p0 = 1
>
> Finally if I use HQL (as much as I dislike using "magic" strings), the
> result is exactly what I would expect and seems the most efficient:
>
>                var fromDb3 = session.CreateQuery("from Vehicle where
> Carrier.Id = :id")
>                    .SetParameter("id", 1)
>                    .List<Vehicle>();
>
> NHibernate: select vehicle0_.Id as Id1_, vehicle0_.VehicleType as
> VehicleT2_1_, vehicle0_.BusinessRef as Business3_1_,
> vehicle0_.CarrierId as CarrierId1_ from Vehicles vehicle0_ where
> vehicle0_.carrier...@p0;@p0 = 1
>
> So why do I get this difference and more importantly, how can I use
> QBC or Linq to generate the same SQL as HQL does?
>
> Thanks,
> Ben
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>


-- 
Fabio Maulo

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to