Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Antlr exception with message "plan b" when walking IQueryable of NHibernate entities

I've got quite weird exception when trying to materialize the IQueryable I got form NHibernate.Linq. The exception of type Antlr.Runtime.Tree.RewriteEmptyStreamException just states plan b, and nothing more. Detailed exception can be found at http://pastebin.com/kR2dvDHd

Here's the code that throws an exception:

var matterExtractor = new MatterExtractor();
var InactiveMatters = matterExtractor.GetMattersAtStatus(General.InactiveMatterStatus);
Assert.IsNotNull(InactiveMatters); //OK
Assert.IsInstanceOfType(InactiveMatters, typeof (IQueryable<Matter>)); // OK
var MaterializedMatters = InactiveMatters.ToList(); //Exception is thrown

Matter Extractor class is as simple as follwing:

public class MatterExtractor
{
    public virtual IQueryable<Matter> GetMattersAtStatus(MatterStatus status)
    {
        return
            (new NHibernateRepository.Repository<Matter>()).Where(
                m => m.MatterStatusHistories.OrderByDescending(msh => msh.CreateTime).FirstOrDefault().MatterStatus == status);
    }
}

NHibernateRepository.Repository<T> is an utility class that implements IQueryable via NHibernate.LINQ extension methods to NHibernate.Session. Nothing specific here, but just in case, here's the listing: http://pastebin.com/MgDxDg3Y

I don't think it's related to NHibernate mappings, since other tests that interact with Matter entity run just fine. Most probably it's related to the Where clause, but I can't understand what's going wrong with that clause. I've tried replacing

OrderByDescending(msh => msh.CreateTime).FirstOrDefault()

to

OrderBy(msh => msh.CreateTime).LastOrDefault()

but it just told me The LastResultOperator result operator is not current supported, so I think NHibernate.Linq just can't stay LastOrDefault.

Any ideas what does plan b mean and how can I workaround it?

like image 272
J0HN Avatar asked Jul 16 '12 17:07

J0HN


1 Answers

Are you certain that OrderByDescending(msh => msh.CreateTime).FirstOrDefault()

Is not returning null for any elements in your repository? That bit of code seems to me to be the bit in question.

(...OrderByDescending(msh => msh.CreateTime).FirstOrDefault() ?? someDummyStatusNotSatisfyingClause)

Might solve your problem.

Another possibility is that you haven't instructed NHibernate how/when to materialiaze the status histories in the entity definition. My experience with NHibernate is that some query like you are attempting might be better suited as a repository function (a stored procedure)

like image 62
Scottley Avatar answered Oct 06 '22 23:10

Scottley