Why do these two Fluent nHibernate queries produce different results? - nhibernate

I've been trying to work out why this query asking for a manager and his or her team only returns the first entry for the Team collection. Apparently, it's because I had FirstOrDefault at the end of the query. I was under the impression the FirstOrDefault would apply to the query as a whole, but it seems it's being applied to the Team collection as well.
Original query (shows only 1st member in Team):
session.Query<IEmployee>()
.Where(p => p.PersonalNumber == PersonalNumber)
.Fetch(p => p.Team)
.Fetch(p => p.Manager)
.FirstOrDefault();
New query which returns full team:
session.Query<IEmployee>()
.Where(p => p.PersonalNumber == PersonalNumber)
.Fetch(p => p.Team)
.Fetch(p => p.Manager)
.ToList().FirstOrDefault();
What would be the correct way to formulate this query? My need for a workaround implies I'm not doing this properly.
Background - mappings:
This is a basic hierarchical relationship with Manager being an IEmployee and Team being an IList of IEmployee.
References(x => x.Manager).Column("ManagerId");
HasMany(x => x.Team)
.AsList(index => index.Column("TeamIndex"))
.KeyColumn("ManagerId");

session.Query<IEmployee>()
.Where(p => p.PersonalNumber == PersonalNumber)
.Fetch(p => p.Team)
.Fetch(p => p.Manager)
.FirstOrDefault();
In this query the FirstOrDefault works on the database as you expected.
session.Query<IEmployee>()
.Where(p => p.PersonalNumber == PersonalNumber)
.Fetch(p => p.Team)
.Fetch(p => p.Manager)
.ToList().FirstOrDefault();
In this query the ToList works on the database. All behind works on the result of the ToList. So the FirstOrDefault gets the FirstOrDefault from the result set of the ToList. To get the same result you will need to add a order to your query. Sql does not grantee the same order of the result set when you do a select without order. The order in the result of ToList is different then the internal order in the first query.

I have been struggling with the same problem. For me, this occurred when upgrading from NH 3.1 -> 3.3. The problem is that with Linq, NHibernate 3.3 generates a SQL query that has a "Top(1)" statement in it, effectively killing the "Fetch"-part of the query. I solved it by switching from Linq to QueryOver. I believe this will work:
session.QueryOver<IEmployee>()
.Where(p => p.PersonalNumber == PersonalNumber)
.Fetch(p => p.Team)
.Fetch(p => p.Manager)
.SingleOrDefault();

The problem is that I am asking for a Cartesian product (using Fetch) but also using FirstOrDefault; from the generated SQL I can see that that combination doesn't work, as you only get the first row of the Cartesian product (SQL generated: “FETCH NEXT 1 ROWS ONLY”).
I will need to write a different type of query if I want to do this, or else just use the ToList workaround which in this instance isn't doing much harm as I'm only expecting one result from the database anyway.
Example solution:
session.QueryOver<IEmployee>()
.Where(p => p.PersonalNumber == PersonalNumber)
.Fetch(p => p.Team).Eager
.Fetch(p => p.Manager).Eager
.SingleOrDefault();

Related

Nhibernate Restrictions.In Error

Finally tracked down my error which is a result of the query. I have an nhibernate query using a Restrictions.In. Problem is once query executes if no results returned query throws error immediately. Is there another restriction that I can use. I know if I was writing a linq query I could use the .Any to return bool value and go from there is there something similar I can do in this instance?
carMake is passed in
myQuery.JoinQueryOver(x => x.Car)
.Where(Restrictions.In("VIN",
Trades.Where(x => x.Car.Make.ToLower() == carMake.ToLower())
.Select(x => x.Car.PrimaryVIN)
.ToList()));
Assuming that Trades is a list of objects you can use .WhereRestrictionOn() instead. Try this (I split the code for better readability):
var vinsWithCarMake = Trades
.Where(x => x.Car.Make.ToLower() == carMake.ToLower())
.Select(x => x.Car.PrimaryVIN)
.ToList<string>();
var carAlias = null;
var result = myQuery.JoinAlias(x => x.Car, () => carAlias)
.WhereRestrictionOn(() => carAlias.VIN).IsInG<string>(vinsWithCarMake)
.List();

QueryOver, GroupBy and Subselect

how can I do this with queryover and a subquery?
FakturaObjektGutschrift fog = null;
int[] idS = Session.QueryOver(() => fog)
.SelectList( list => list
.SelectMax(() => fog.Id)
.SelectGroup(() => fog.SammelKontierung)
).List<object[]>().Select(x => (int)x[0]).ToArray();
And using the ids in another query
IQueryOver<Beleg, Beleg> sdfsd = Session.QueryOver(() => bGut)
.AndRestrictionOn(() => fog.Id).IsIn(idS); //iDs is a list of int.
I would like do this with a subquery because there is a limitation on the number of parameters for a SQL query. How do I do this?
How do I write the first query, but without selecting the SelectGroup()? This is exactly where I got stuck.
Group by without projection in QueryOver API is currently not supported link.
You can use LINQ to create the projection in a subquery:
var subquery = Session.Query<FakturaObjektGutschrift>()
.GroupBy(x => x.SammelKontierung)
.Select(x => x.Max(y => y.Id));
var query = Session.Query<Beleg>()
.Where(x => subquery.Contains(x.Id));
If you really needs QueryOver to create more complex queries check this solution link.

QueryOver fails with could not resolve property:

I am using NHibernate 3.0 and was comparing Query and QueryOver
var p = _prepo.Query<Party>()
.Where(c => c.Person.LastName == "Bobby")
.FirstOrDefault();
The above works, I get proxy class for p.Person if I view the object graph.
var p = _prepo.QueryOver<Party>()
.Where(c => c.Person.LastName == "Bobby")
.FirstOrDefault();
This one fails with error ==> could not resolve property: Person.LastName of:
Why?
I'm not familiar with the Linq provider but when using QueryOver you have to use a join to do a query like that:
Example 1
IQueryOver<Cat,Kitten> catQuery =
session.QueryOver<Cat>()
.JoinQueryOver(c => c.Kittens)
.Where(k => k.Name == "Tiddles");
Example 2
Cat catAlias = null;
Kitten kittenAlias = null;
IQueryOver<Cat,Cat> catQuery =
session.QueryOver<Cat>(() => catAlias)
.JoinAlias(() => catAlias.Kittens, () => kittenAlias)
.Where(() => catAlias.Age > 5)
.And(() => kittenAlias.Name == "Tiddles");
It works when you use Linq in this case because the filtering is being done on the client, not in the database. So it is actually the IEnumerable version of Where that is running which is not related to NHibernate.
The QueryOver uses Expression<Func<T,object>> which NHibernate tries to translate to SQL but fails. For reasons unknown to me you must explicitly join using JoinQueryOver or JoinAlias.
Some more info on QueryOver here:
http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html

NHibernate - possible to fetch a collection after querying?

There's sooo much literature about fetching and eager loading when doing the actual query using .Fetch
But, once I have a loaded entity - with an empty collection (because I chose not to eager load at query time due to the cartesian product side-effect), can I choose to load a collection a bit later on, say after I've done some paging and I have a concrete List of items?
something like:
var list = (some linq over Session.Query<Entity>)
.Take(10).Skip(2)
.Fetch(x => x.MyCollection)
.ToList();
Session.Fetch<Entity>(list, l => l.OtherCollection);
Edit
The point is - i'm already fetching 2 child collections in the Query - makes the query and result set quite sizeable already (see nhibernate Cartesian product). I'd like page the results, get a list of 10 then optionally go back to the database to populate child collection properties of the paged (10, say) result. This is a performance consideration.
Issue this query
/*we dont need the result*/Session.QueryOver<Entity>()
.Where(x => x.Id.IsIn(list.Select(l => l.Id)))
.Fetch(l => l.OtherCollection)
.ToList();
then nhibernate should initialize the collections on the Entities
EDIT:
to improve initial loading time see http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate
then you can do for exmaple
var results = (some linq over Session.Query<Entity>)
.Take(10).Skip(2)
.ToList();
var q = Session.QueryOver<Entity>()
.Where(x => x.Id.IsIn(list.Select(l => l.Id)))
.Fetch(l => l.MyCollection)
.ToFuture();
Session.QueryOver<Entity>()
.Where(x => x.Id.IsIn(list.Select(l => l.Id)))
.Fetch(l => l.OtherCollection)
.ToFuture();
Session.QueryOver<Entity>()
.Where(x => x.Id.IsIn(list.Select(l => l.Id)))
.Fetch(l => l.ThirdCollection)
.ToFuture();
return q.ToList()
I've just gone off to read about futures and projections in NHibernate which look promising as a solution...will post more when I find out about it.
This is a solution: http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate, but I still do not like it, as my query itself is quite expensive (uses '%like%# + paging), so executing 3 or 4 times just to load collections seems expensive
Edit
This is what I have. Lookign at the sql generated, the correct sql is being run and returning expected results, but the collections on the returned results are null. Can you see what's missing?:
public List<Company> CompaniesForLoggedInUser(int pageSize, int pageNumber)
{
var list =
QueryForCompaniesFor(SecurityHelper.LoggedInUsername)
.Page(pageNumber, pageSize)
.ToList()
.FetchCompanyCollections(Session);
return list;
}
internal static class CompanyListExtensions
{
internal static List<Company> FetchCompanyCollections(this List<Company> companies, ISession session)
{
var ids = companies.Select(l => l.Id).ToArray();
session.QueryOver<Company>()
.Where(x => x.Id.IsIn(ids))
.Fetch(l => l.Properties).Eager()
.Future();
return session.QueryOver<Company>()
.Where(x => x.Id.IsIn(ids))
.Fetch(l => l.UserAccessList).Eager()
.Future()
.ToList();
}
}

Is this the right way of using ThenFetch() to load multiple collections?

I'm trying to load all the collections eagerly, using NHibernate 3 alpha 1. I'm wondering if this the right way of using ThenFetch()?
Properties with plural names are collections. The others are just a single object.
IQueryable<T> milestoneInstances = Db.Find<T, IQueryable<T>>(db =>
from mi in db
where mi.RunDate == runDate
select mi).Fetch(mi => mi.Milestone)
.ThenFetch(m => m.PrimaryOwners)
.Fetch(mi => mi.Milestone)
.ThenFetch(m => m.SecondaryOwners)
.Fetch(mi => mi.Milestone)
.ThenFetch(m => m.Predecessors)
.Fetch(mi => mi.Milestone)
.ThenFetch(m => m.Function)
.Fetch(mi => mi.Milestone)
.ThenFetchMany(m => m.Jobs)
.ThenFetch(j => j.Source)
;
I thought of asking this in the NHibernate forums but unfortunately access to google groups is forbidden from where I am. I know Fabio is here, so maybe the guys from the NHibernate team can shed some light on this?
Thanks
Apparently, there's no "right" way to use ThenFetch in such a case. Your example works fine but SQL produced contains many joins to Milestone, which isn't that right.
Using IQueryOver instead of IQueryable allows you to use complex syntax in Fetch:
Fetch(p => p.B)
Fetch(p => p.B.C) // if B is not a collection ... or
Fetch(p => p.B[0].C) // if B is a collection ... or
Fetch(p => p.B.First().C) // if B is an IEnumerable (using .First() extension method)
So in your case it would be:
query // = session.QueryOver<X>()
.Fetch(mi => mi.Milestone).Eager
.Fetch(mi => mi.Milestone.PrimaryOwners).Eager
.Fetch(mi => mi.Milestone.SecondaryOwners).Eager
.Fetch(mi => mi.Milestone.Predecessors).Eager
.Fetch(mi => mi.Milestone.Function).Eager
.Fetch(mi => mi.Milestone.Jobs).Eager
.Fetch(mi => mi.Milestone.Jobs.First().Source).Eager
The one thing you are missing is that you should use FetchMany() and ThenFetchMany() is the child property is a collection.
IQueryable<T> milestoneInstances = Db.Find<T, IQueryable<T>>(db =>
from mi in db
where mi.RunDate == runDate
select mi);
var fetch = milestoneInstances.Fetch(f => f.Milestone);
fetch.ThenFetch(f => f.PrimaryOwners);
fetch.ThenFetch(f => f.SecondaryOwners);
//...
As leora said, make sure when fetching children collections that you use
FetchMany()
ThenFetchMany()
A new Fetch, should pick up from the root, but this does not always happen. Sometimes you need to create them as separate queries or use Criteria Futures to batch up a multiple fetch.