我在NHibernate遇到了一个叫Queryover的JoinAlias的意外行为。
我的实体基本上是这样的:

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
}

我的地图是这样的:
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()
            ;
    }

我的问题:
        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))
            ;

就我而言,这应该会给我一些这样的sql:
…其中(a.parentfieldid==null)或(a.parentfieldid=c.fieldid和c.value=parentvalue)
但我得到了一个空引用异常。(我假设别名解析后父项为空)。
异常详细信息如下:
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:

我尝试了几种不同的方法,包括:
            .JoinAlias(() => fieldAlias.Parent, () => parentFieldAlias)
            .Where(Restrictions.Or(Restrictions.On(() => fieldAlias.Parent).IsNotNull,
                Restrictions.On(() => parentFieldAlias.Value).IsLike(parentValue))))

但这些都不管用。
我有一个解决方法,在查询中去掉对父字段值的限制,并在queryover返回后执行linq查询,如下所示:
        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();

但这并不像在Queryover中那样理想。
有什么建议吗?

最佳答案

有限制的第二个解决方案可以完成这项工作。有两个问题。我们需要一个左连接到父节点,我看到一个输入错误:IsNotNull应该是IsNull以正确评估OR语句:
破解IsNotNull(以及最可能的内部连接)

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

使用OR和左连接工作:
.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))
);

第一个解决方案的问题是,我们无法计算浮动/虚拟对象是否为空:IsNull
。其中(()=>(parentFieldAlias.value==parentValue)||
(parentFieldAlias==null));
我们需要检查所有者的财产:parentFieldAlias

09-26 13:54