2
votes

I have two entities that are connected through one to many relationship. like this example:

public class Category
{
    public int Id {get; set; }
    public string Name {get; set;}
}

public class Product 
{
    public int Id {get; set;}
    public string Name {get; set;}
    public Category ProductCategory {get; set;}
}

and the mapping using fluent nhibernate

public class CategoryMap : ClassMap<Category>
{
....
}

public Class ProductMap: ClassMap<Product>
{
...
References(x => x.ProductCategory).Column("CategoryId");
}

When I do search using linq to NHibernate with where clause and equal, it generates inner join between product and category

so this

var query = session.Query<Product>()
     .Where (x => x.ProductCategory.Name == "abc");

this will generate inner join

but when I do search with startwith like this

var query = session.Query<Product>()
     .Where (x => x.ProductCategory.Name.StartWith("abc"));

this will generate outer join between the two.

Why, and how can I force to generate inner join?

1
QueryOver let you to choose between them - Najera

1 Answers

0
votes

You can do it using QueryOver, which is another method to create query in NHibernate. In that case, you specify the join type as you want.

Category category = null;

var result = session.QueryOver<Product>()
                    .JoinAlias(x => x.ProductCategory, () => category, JoinType.InnerJoin)
                    .Where(Restrictions.Like(Projections.Property(() => category.Name), "abc%", MatchMode.Start))
                    .List();

On the other hand, query over is more verbose code, you have to specify a lot of things you avoid using linq.