NHibernate QueryOver with ManytoMany
Asked Answered
H

1

6

I'm in the process of learning QueryOver, but I can't for my life figure out how to do simple many to many queries.

I've written the following:

            var result = Session.CreateCriteria(typeof (Product))
                .CreateAlias("Categories", "categories")
                .Add(Property.ForName("categories.Id").Eq(categoryId))
                .List<Product>();

This achieves the desired result. Basically I have

Product > ProductCategory < Category

ProductCategory just has ProductId / CategoryId, and I'm trying to select all the products in a specific category.

I have no idea where to start with trying to do this with queryover.

Hulbert answered 10/10, 2010 at 14:53 Comment(0)
H
15

I ended up resolving this after a lot of perseverance.

            var result = Session.QueryOver<Product>()
                            .Right.JoinQueryOver<Category>(x => x.Categories)
                            .Where(c => c.Id == categoryId)
                            .List();

What a mission :)

Hulbert answered 11/10, 2010 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.