Thanks a lot Symon. That's exactly what I needed.
I have the performance enhancement that I needed, but now there is one problem with the result set. If a user has 3 roles, then that user will be duplicated in the result list 3 times. I tried several approaches to fix this. Adding DISTINCT to my SELECT clause didn't seem to have any impact, and neither did using NHibernate's DistinctRootEntityResultTransformer.
I ended up writing my own method to eliminate the duplicate entries from the List that my query returns, but I was wondering if there is a better way for NHibernate to do this for me. Does anyone have any suggestions?
Here is the hql:
Code:
SELECT DISTINCT u, d, c
FROM User u
INNER JOIN FETCH u.Roles
LEFT JOIN u.Division d
LEFT JOIN u.Division.Customer c
WHERE (:division IS NULL OR d = :division)
AND (:customerId = 0 OR c.Id = :customerId)
AND (:name IS NULL OR u.Name LIKE '%' + :name + '%')
AND (:onlyActive = false OR u.IsActive = true)
AND (:onlyActive = false OR d.IsActive = true)
AND (:onlyActive = false OR c.IsActive = true)
ORDER BY u.Division.Name, u.IsActive DESC, u.Name
and here is the log entry I got back from using the DistinctRootEntityResultTransformer:
15:33:08.440 [2624] DEBUG NHibernate.Transform.DistinctRootEntityResultTransformer - transformed: 388 rows to: 388 distinct results
It says that all the rows were already distinct but when I look inside the actual collection, my users are in fact duplicated.
Thanks for your help.