Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:21:44+00:00 2026-06-15T16:21:44+00:00

I’m getting some unexpected behaviour with a JoinAlias QueryOver in NHibernate. My entity essentially

  • 0

I’m getting some unexpected behaviour with a JoinAlias QueryOver in NHibernate.

My entity essentially looks like this:

public class Field
{
    public virtual long Id { get; protected set; }
    public virtual Field Parent { get; protected set; }
    public virtual FieldType Type { get; protected set; }
    public virtual string Value { get; protected set; }

...(Ctors etc
}

My mapping is such:

public class FieldMap : ClassMap<Field>
{
    public FieldMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Native();

        References(x => x.Type)
            .Column("FieldTypeId")
            .LazyLoad()
            .Cascade.All()
            ;

        Map(x => x.Value);

        References(x => x.Parent)
            .Column("ParentFieldId")
            .Nullable()
            .LazyLoad()
            .Cascade.All()
            ;
    }

My query:

        Field fieldAlias = null;
        string typeAlias = null;
        Field parentFieldAlias = null;

        var query = getSession().QueryOver<Field>(() => fieldAlias)
            .JoinAlias(() => fieldAlias.Type, () => typeAlias)
            .Where(() => typeAlias.Name == type)
            .JoinAlias(() => fieldAlias.Parent, () => parentFieldAlias)
            .Where(() => (parentFieldAlias.Value == parentValue) || (parentFieldAlias == null))
            ;

As far as I’m concerned, this should give me some SQL like this:

… WHERE (a.ParentFieldId == NULL) OR (a.ParentFieldId = c.FieldId AND c.Value = parentValue)

But I’m getting a null reference exception. (I assume when the alias is resolved and the Parent is null).

The exception details are:

System.NullReferenceException occurred
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=NHibernate
  StackTrace:
       at NHibernate.Criterion.ConstantProjection..ctor(Object value)
       at NHibernate.Criterion.Projections.Constant(Object obj)
       at NHibernate.Impl.ExpressionProcessor.FindMemberProjection(Expression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessSimpleExpression(Expression left, Expression right, ExpressionType nodeType)
       at NHibernate.Impl.ExpressionProcessor.ProcessSimpleExpression(BinaryExpression be)
       at NHibernate.Impl.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessOrExpression(BinaryExpression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessLambdaExpression(LambdaExpression expression)
       at NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression`1 expression)
       at NHibernate.Criterion.QueryOver`2.Add(Expression`1 expression)
       at NHibernate.Criterion.QueryOver`2.Where(Expression`1 expression)
       at NHibernate.Criterion.QueryOver`2.NHibernate.IQueryOver<TRoot,TSubType>.Where(Expression`1 expression)
       at Ismoos.Director.FieldOptionsQuery.Execute(Service service, String type, String parentValue) in D:\Work\Ismoos\Ismoos\Director\Ismoos.Director\FieldOptionsQuery.cs:line 31
  InnerException:

I’ve tried a few different ways, including:

            .JoinAlias(() => fieldAlias.Parent, () => parentFieldAlias)
            .Where(Restrictions.Or(Restrictions.On(() => fieldAlias.Parent).IsNotNull,
                Restrictions.On(() => parentFieldAlias.Value).IsLike(parentValue))))

but none of these work.

I have a workaround, by leaving out the restriction on the parent field value in the query, and performing a LINQ query after the QueryOver returns, like so:

        Field fieldAlias = null;
        string typeAlias = null;
        Field parentFieldAlias = null;

        var query = getSession().QueryOver<Field>(() => fieldAlias)
            .JoinAlias(() => fieldAlias.Type, () => typeAlias)
            .Where(() => typeAlias.Name == type)
            ;

        var list = query
        .List<Field>()
        ;

        return list
            .Where(x => (x.Parent == null) || (x.Parent.Value == parentValue))
            .ToList();

but this isn’t as optimal as doing it in the QueryOver.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:21:45+00:00Added an answer on June 15, 2026 at 4:21 pm

    The second solution with restrictions would do the job. There are two issues. We need a LEFT JOIN to Parent and I see a typo: IsNotNull should be IsNull to correctly evaluate the OR statement:

    Broken solution IsNotNull (and most likely inner join)

    .JoinAlias(() => fieldAlias.Parent, () => parentFieldAlias)
    .Where
    (
      Restrictions.Or(
        Restrictions.On(() => fieldAlias.Parent).IsNotNull, // here
        Restrictions.On(() => parentFieldAlias.Value).IsLike(parentValue)))
    )
    

    Working OR with IsNull and LEFT JOIN:

    .JoinAlias(() => fieldAlias.Parent, () => parentFieldAlias
               , NHibernate.SqlCommand.JoinType.LeftOuterJoin)) // left join for NULL
    .Where
    (
      Restrictions.Or(
        Restrictions.On(() => fieldAlias.Parent).IsNull, // this is what we need
        Restrictions.On(() => parentFieldAlias.Value).IsLike(parentValue))
    );
    

    The issue with the first solution is, that we cannot evaluate for null the floating/virtual object: parentFieldAlias

    .Where(() => (parentFieldAlias.Value == parentValue) ||
    (parentFieldAlias == null)) ;

    We need to check the property of the owner: fieldAlias.Parent

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.