我在 NHibernate 中使用 JoinAlias QueryOver 得到一些意外行为。
我的实体基本上看起来像这样:
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:
... WHERE (a.ParentFieldId == NULL) 或 (a.ParentFieldId = c.FieldId AND c.Value = parentValue)
但我得到一个空引用异常。(我假设当别名被解析并且 Parent 为空时)。
异常详情如下:
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 中那样优化。
有什么建议么?