NHibernate 3CR1: Fetching strategy problems

0 votes
asked Nov 25, 2010 by ari

Continuation to the previous question, I tried to avoid the problem another way:

Just for the reminder:

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

EDIT: 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.ResultOperatorProcessor1.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(ObservableCollection1 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, IDictionary2 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, IDictionary2 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, IDictionary2 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, IDictionary2 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, IDictionary2 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.QueryableBase1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 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

1 Answer

0 votes
answered Nov 28, 2012 by isaac-bolinger

I think Nhibernate doesn't like you fetching after a select. I ran into a null reference exception just like you did.

See my question here about it.

In Linq-to-Nhibernate, is it possible to use .Fetch() after a .Select()?

You might be able to .Select(x => x.Form) after your fetching in your second example before you .ToList() it, too.

I'm using Nhibernate 3.2 and this is still an issue. There should be a proper 'you can't fetch after a select' exception or something to spare us some pain.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...