I wonder if this can be resolved with less overhead:
Given a simple one-to-many relationship Product --> Size (Pro开发者_C百科duct has got one size). In order to figure out how many products are assigned to a size I would update the mapping of Size with a Product-Bag
. But what if I am only interested in the count (no need for any product details), can this be done without the overhead of loading all the product-objects?
Thx for any tipps sl3dg3
Use attribute lazy="extra"
in hbm or ExtraLazyLoad()
in fluent mappings for Product collection.
With extra lazy loading Products.Count translates into sql 'select count'
See corresponding question
Why not create a query? Something like this for Linq (of course HQL, criteria or QueryOver should work too):
int count = session.Query<Product>()
.Where(x => x.Size != null)
.Count();
精彩评论