and about "magic strings"... put it in the mapping and NH'll compile it at BuildSessionFactory and you don't have "magic strings" anymore.
On Thu, Jun 24, 2010 at 12:34 PM, Fabio Maulo <[email protected]> wrote: > btw... I don't know where you are generating the query... in general I > don't have such problems because I'm using HQL most of the times. > > > On Thu, Jun 24, 2010 at 12:24 PM, Diego Mijelshon > <[email protected]>wrote: > >> session.Query<T> is new in 3.x, using the integrated Linq provider, which >> is HQL-based instead of Criteria-based behind the scenes, allowing for more >> flexibility. >> >> Diego >> >> >> >> On Thu, Jun 24, 2010 at 12:13, ben <[email protected]> wrote: >> >>> Fabio, >>> >>> Is session.Query<T> new? >>> >>> Using what you have with session.Linq<T>: >>> >>> var query = from v in _session.Linq<Vehicle>() >>> where v.Carrier == >>> _session.Load<Carrier>(carrierId) >>> select v; >>> >>> Generates the same sql as what I have in the my original Linq example: >>> >>> 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_, carrier1_.Enabled as >>> Enabled2_0_ FROM Vehicles this_ left outer join Carriers carrier1_ on >>> this_.CarrierId=carrier1_.Id WHERE this_.CarrierId = @p0;@p0 = 1 >>> >>> Thanks >>> Ben >>> >>> On Jun 23, 12:38 pm, Fabio Maulo <[email protected]> wrote: >>> > 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]> >>> <nhusers%[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]<nhusers%[email protected]> >>> . >>> For more options, visit this group at >>> http://groups.google.com/group/nhusers?hl=en. >>> >>> >> -- >> 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 > > -- 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.
