Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate sub-collection using QueryOver

Tags:

nhibernate

I have simple classes:

public class Order
{
    public int Id {get;set;}
    public IList<Name> Names{get;set;}
}

public class Name
{
    public int Id {get;set;}
    public int LangId {get;set;}
    public string LocalName {get;set;}
}

The quesetion is how to query subcollection of Order to get all that have some Order.Names.LocalName (something like this):

IList<Order> orders = repository.GetByProductLocalName("laptop");

I would like to use new NH3 feature QueryOver, not using Linq (I'v found some solutions in SO but all of them uses Linq).

like image 385
Dariusz Avatar asked Jan 29 '26 23:01

Dariusz


1 Answers

All you need to do is declare an alias variable for the Names collection and then you can use that in the where clause...

String localName = "laptop";
Name namesAlias = null;

return session.QueryOver<Order>()
    .JoinAlias(x => x.Names, () => namesAlias)
    .Where(() => namesAlias.LocalName == localName)
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Order>();
like image 92
dotjoe Avatar answered Feb 01 '26 13:02

dotjoe