James,

Thanks for the reply.  And great work on Fluent Nhibernate and your
series of accompanying blog posts.  They've been very helpful for us.

We were hoping to do most things using criteria but I think this
situation has told us that we probably need to have a good strategy in
place for using HQL when necessary.  We did work out a way to get the
product results using a subquery but it still seems odd that I can get
the list if product IDs using a projection but not the products
themselves but I may be missing something very fundamental.

Anyway, we ended up with something like this for the subquery route
(using NHLambdaExtensions):

 var prodIds = DetachedCriteria.For<Category>()
                .Add<Category>(c => c.ID == categoryId)
                .CreateCriteria<Category>(c => c.Products,() =>
productAlias)
                    .SetProjection(Projections.Distinct
(LambdaProjection.Property(() => productAlias.ID)));

            var crit = DetachedCriteria.For<Product>();
            crit.Add(LambdaSubquery.Property<Product>(p => p.ID).In
(prodIds));

Probably not a great overall strategy but we're still basically
learning nhibernate and wanted to determine how to do it.

On Feb 4, 1:22 pm, James Gregory <[email protected]> wrote:
> I don't know about with a criteria, but with hql you could do something like
> this (from memory): "select p from Category c join Product p where c.Id =
> :id"
>
> On Wed, Feb 4, 2009 at 6:12 PM, Kevin <[email protected]> wrote:
>
> > A coworker and I are trying to get a sense of how to best handle
> > certain queries in situations where we have a many-to-many mapping
> > that is exposed only on one side of the relationship.  The questions
> > we had were in response to some design ideas from this article:
>
> >http://www.udidahan.com/2009/01/24/ddd-many-to-many-object-relational...
>
> > The general idea being that we have the following classes:
>
> > - Two classes. Product and Category.
> > - Category has a list of Products.
> > - Product doesn't even know Category exists.
>
> > The relationship at a database level would be implemented by a linking
> > table but in our domain model we'd prefer to only expose the
> > relationship in the Category class.  The question is : What is the
> > best way to build a query returning all products for a given
> > category?  We specifically want to have this result as some sort of
> > criteria since we would like to be able to pass the criteria (actually
> > a wrapper class) to other levels of the application for sorting and
> > paging.
>
> > At this point, we are using fluent nhibernate, automapping with a
> > specific override for the category class as follows:
>
> >  mappings.ForTypesThatDeriveFrom<Category>(x =>
> > x.HasManyToMany<Product>(y => y.Products));
>
> > In terms of possible options for querying, we've really just come up
> > with using a subquery against the category to get a list of product
> > IDs which can then be used with an IN clause on the products.  The
> > other possibility that we considered would be to expose the
> > relationship on the other end as a private or protected property.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to