Let’s say I have the following 2 entities. (Please forgive any code typos, I am being a bit of an insomniac and and typing this from memory)
public class Foo {
public virtual int Id {get;set;}
public virtual int Version {get;set;}
public virtual string SomeProp {get;set;}
public virtual Bar Bar {get;set;}
}
public class Bar {
public virtual int Id {get;set;}
public virtual int Version {get;set;}
public virtual string Name {get; set;}
}
//Fluent Mappings
public class FooMapping :ClassMap<Foo>
{
public FooMapping()
{
Id (f => f.Id).GeneratedBy.Identity();
Version(f => f.Version);
Map(f=> f.SomeProp).Column("fooprop1");
References(f => f.Bar).Column("foobarid").Not.Null();
}
}
public class BarMapping :ClassMap<Bar>
{
public BarMapping()
{
Id (b => b.Id).GeneratedBy.Identity();
Version(b => b.Version);
Map(b => b.name).Column("barname");
}
}
when I write a query like this:
var query = from f in Session.Query<Foo>()
where f.Bar.Id == 5
select new {f.Id, f.SomeProp};
I notice that NHibernate generates a sql query with an inner join to the Bar table, and then does it’s where clause on the Bar table’s Id. I suspect it does this to ensure that the bar.id=5 is a valid bar id?
My question is how can I tell NHibernate to let me query the foo table alone against the foo.foobarid column. Something like this:
SELECT f.id, f.prop1 FROM foo f WHERE f.foobarid = 5
Thank you in advance for any insight you can provide!
Seems like this optimization is not implemented for Linq (it’s documented and working for HQL).
But you can use a
Barobject for the comparison as a workaround.session.load()will not hit the DB, so there is no additional performace cost for that.