NHibernate 3CR1: Fetching strategy poblems

Description

Hello,

> My database schema is described below:
>
> Form <-> Log
>
> <--->>Seller1
>
> <--->>Seller2
>
> <--->>Seller3
>
> I have a major entity (Form), one to
> one relationship to another object
> (Log) And one to many relationship to
> the childs (Sellers).
>
> I want to pull out all the Forms that
> one of their Sellers meets certain
> conditions.

I tried like this now:

[Test]
public void Can_Get_Forms_Where_CorporationNumber_Is_510778087_Metohd1()
{
var CorporationNumber = "513514950";

var list1 = sellerRepository
.Where(x => x.CorporationNumber == CorporationNumber)
.Select(x => x.Form)
.Fetch(x => x.Log)
.Take(10).ToList();

CollectionAssert.IsNotEmpty(list1);
}
But unfortunately I get NullReferenceException:

> TestUsingDevelopmentDataBase.Moch.BillOfSale.Data.FormRepositoryTests.Can_Get_Forms_Where_CorporationNumber_Is_510778087_Metohd1:
> System.NullReferenceException : Object
> reference not set to an instance of an
> object

stacktrace:

> at
> NHibernate.Linq.Visitors.ResultOperatorProcessors.ProcessFetch.Process(FetchRequestBase
> resultOperator, QueryModelVisitor
> queryModelVisitor, IntermediateHqlTree
> tree) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\ResultOperatorProcessors\ProcessFetch.cs:line
> 11 at
> NHibernate.Linq.Visitors.ResultOperatorProcessors.ProcessFetchOne.Process(FetchOneRequest
> resultOperator, QueryModelVisitor
> queryModelVisitor, IntermediateHqlTree
> tree) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\ResultOperatorProcessors\ProcessFetchOne.cs:line
> 9 at
> NHibernate.Linq.Visitors.ResultOperatorProcessors.ResultOperatorProcessor`1.Process(ResultOperatorBase
> resultOperator, QueryModelVisitor
> queryModel, IntermediateHqlTree tree)
> in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\ResultOperatorProcessors\ResultOperatorProcessor.cs:line
> 17 at
> NHibernate.Linq.Visitors.ResultOperatorProcessors.ResultOperatorMap.Process(ResultOperatorBase
> resultOperator, QueryModelVisitor
> queryModel, IntermediateHqlTree tree)
> in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\ResultOperatorProcessors\ResultOperatorMap.cs:line
> 24 at
> NHibernate.Linq.Visitors.QueryModelVisitor.VisitResultOperator(ResultOperatorBase
> resultOperator, QueryModel queryModel,
> Int32 index) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line
> 125 at
> Remotion.Data.Linq.Clauses.ResultOperatorBase.Accept(IQueryModelVisitor
> visitor, QueryModel queryModel, Int32
> index) at
> Remotion.Data.Linq.QueryModelVisitorBase.VisitResultOperators(ObservableCollection`1
> resultOperators, QueryModel
> queryModel) at
> Remotion.Data.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel
> queryModel) at
> NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
> in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line
> 96 at
> NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel
> queryModel, VisitorParameters
> parameters, Boolean root) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line
> 49 at
> NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor
> sessionFactory) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhLinqExpression.cs:line
> 67 at
> NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String
> queryIdentifier, IQueryExpression
> queryExpression, String
> collectionRole, Boolean shallow,
> IDictionary`2 filters,
> ISessionFactoryImplementor factory) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\ASTQueryTranslatorFactory.cs:line
> 27 at
> NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String
> expressionStr, IQueryExpression
> queryExpression, String
> collectionRole, Boolean shallow,
> IDictionary`2 enabledFilters,
> ISessionFactoryImplementor factory) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Engine\Query\HQLExpressionQueryPlan.cs:line
> 34 at
> NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String
> expressionStr, IQueryExpression
> queryExpression, String
> collectionRole, Boolean shallow,
> IDictionary`2 enabledFilters,
> ISessionFactoryImplementor factory) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Engine\Query\HQLExpressionQueryPlan.cs:line
> 23 at
> NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String
> expressionStr, IQueryExpression
> queryExpression, Boolean shallow,
> IDictionary`2 enabledFilters,
> ISessionFactoryImplementor factory) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Engine\Query\HQLExpressionQueryPlan.cs:line
> 17 at
> NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression
> queryExpression, Boolean shallow,
> IDictionary`2 enabledFilters) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Engine\Query\QueryPlanCache.cs:line
> 88 at
> NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression
> queryExpression, Boolean shallow) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Impl\AbstractSessionImpl.cs:line
> 302 at
> NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression
> queryExpression) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Impl\AbstractSessionImpl.cs:line
> 258 at
> NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression
> expression, IQuery& query,
> NhLinqExpression& nhQuery) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:line
> 42 at
> NHibernate.Linq.NhQueryProvider.Execute(Expression
> expression) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:line
> 25 at
> NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression
> expression) in
> d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:line
> 102 at
> Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
> at
> System.Collections.Generic.List`1..ctor(IEnumerable`1
> collection) at
> System.Linq.Enumerable.ToList[TSource](IEnumerable`1
> source) at
> TestUsingDevelopmentDataBase.Moch.BillOfSale.Data.FormRepositoryTests.Can_Get_Forms_Where_CorporationNumber_Is_510778087_Metohd1()
> in
> D:\Dev\NCommon\Moch.BillOfSale\Moch.BillOfSale.NHibenate.Tests\FormRepositoryTests.cs:line
> 207

The problem could be circumvented in a less well, as follows:

[Test]
public void Can_Get_Forms_Where_CorporationNumber_Is_510778087_Metohd2()
{
var CorporationNumber = "513514950";

var list2 = sellerRepository
.Where(x => x.CorporationNumber == CorporationNumber)
.Fetch(x => x.Form).ThenFetch(x => x.Log)
.Take(10).ToList().Select(x => x.Form);

CollectionAssert.IsNotEmpty(list2);
}

But of course we all prefer the elegant way and want to understand what lies behind the problem

Thanks. ari

Environment

None

Assignee

Unassigned

Reporter

ari

Labels

None

Components

Affects versions

Priority

Critical
Configure