Nhibernate为获取生成OUTER JOIN

尽管将映射设置为Not.Nullable()Not.LazyLoad()

出于某种原因,NH正在连接一个表两次,一次使用INNER JOIN来安抚WHERE,其次使用OUTER JOIN来选择数据。

当然,因为我们已经加入了数据,所以只使用连接表是有意义的...

SELECT
     ...Tables..
from Tasks taskentity0_,
 outer Cases caseentity1_,
 outer Grades gradeentit2_,
 Cases caseentity5_ 
WHERE
....

我的LINQ查询是这样的:

IQueryable<TaskEntity> tasks = TaskRepo.Find(
    t => t.DueDate <= DateTime.Now
      && (t.TaskInitials == userInitials || (t.TaskInitials == "" || t.TaskInitials == null))
      && t.Team.GST.Any
                    (x => x.Initials == userInitials
                        && x.WorkType.WorkTypeCode == t.WorkType.WorkTypeCode
                        && x.Team.TeamCode == t.Team.TeamCode
                    )
      && (t.Case.CaseOnHold <= DateTime.Now || t.Case.CaseOnHold == null || (t.SingleTask == "M" || t.SingleTask == "m"))
      && (t.Case.CaseMatter.StartsWith("0") || t.Case.CaseMatter.StartsWith("9"))
                ).Fetch(t => t.Case,FetchProvider)

我的参考映射:

        References(x => x.Case).Column("ta_c_ref").Not.Nullable();

思考?

我们正在使用存储库模式,并重新实现了Fetch扩展方法以这种方式工作(因此将FetchProvider传入)。

此外,因为我们需要IQueryable s, QueryOver<T>不是一个选项。

我正在使用NH 3.1。

对于群众:

我们不再使用Fetch或LINQ,我们转移到HQL ...

    /// <summary>
    /// Interfaces for Fetch() statements
    /// </summary>

    public interface IFetchingProvider
    {
        IFetchRequest<TOriginating, TRelated> Fetch<TOriginating, TRelated>(IQueryable<TOriginating> query, Expression<Func<TOriginating, TRelated>> relatedObjectSelector);

        IFetchRequest<TOriginating, TRelated> FetchMany<TOriginating, TRelated>(IQueryable<TOriginating> query, Expression<Func<TOriginating, IEnumerable<TRelated>>> relatedObjectSelector);

        IFetchRequest<TQueried, TRelated> ThenFetch<TQueried, TFetch, TRelated>(IFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, TRelated>> relatedObjectSelector);

        IFetchRequest<TQueried, TRelated> ThenFetchMany<TQueried, TFetch, TRelated>(IFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, IEnumerable<TRelated>>> relatedObjectSelector);
    }


public class NhFetchingProvider : IFetchingProvider
    {
        public IFetchRequest<TOriginating, TRelated> Fetch<TOriginating, TRelated>(IQueryable<TOriginating> query, Expression<Func<TOriginating, TRelated>> relatedObjectSelector)
        {
            var fetch = EagerFetchingExtensionMethods.Fetch(query, relatedObjectSelector);
            return new FetchRequest<TOriginating, TRelated>(fetch);
        }

        public IFetchRequest<TOriginating, TRelated> FetchMany<TOriginating, TRelated>(IQueryable<TOriginating> query, Expression<Func<TOriginating, IEnumerable<TRelated>>> relatedObjectSelector)
        {
            var fecth = EagerFetchingExtensionMethods.FetchMany(query, relatedObjectSelector);
            return new FetchRequest<TOriginating, TRelated>(fecth);
        }

        public IFetchRequest<TQueried, TRelated> ThenFetch<TQueried, TFetch, TRelated>(IFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, TRelated>> relatedObjectSelector)
        {
            var impl = query as FetchRequest<TQueried, TFetch>;
            var fetch = EagerFetchingExtensionMethods.ThenFetch(impl.NhFetchRequest, relatedObjectSelector);
            return new FetchRequest<TQueried, TRelated>(fetch);
        }

        public IFetchRequest<TQueried, TRelated> ThenFetchMany<TQueried, TFetch, TRelated>(IFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, IEnumerable<TRelated>>> relatedObjectSelector)
        {
            var impl = query as FetchRequest<TQueried, TFetch>;
            var fetch = EagerFetchingExtensionMethods.ThenFetchMany(impl.NhFetchRequest, relatedObjectSelector);
            return new FetchRequest<TQueried, TRelated>(fetch);
        }
}

public static IFetchRequest<TOriginating, TRelated> Fetch<TOriginating, TRelated>(this IQueryable<TOriginating> query, Expression<Func<TOriginating, TRelated>> relatedObjectSelector, Func<IFetchingProvider> FetchingProvider)
        {
            return FetchingProvider().Fetch(query, relatedObjectSelector);
        }

在NHibernate中使用linq内部连接还不支持。 更多信息可以在这里找到:https://nhibernate.jira.com/browse/NH-2790


我已经添加了对它的支持!

https://www.nuget.org/packages/NHibernate.Linq.InnerJoinFetch

请享用!!!

链接地址: http://www.djcxy.com/p/60997.html

上一篇: Nhibernate generating OUTER JOIN for a fetch

下一篇: struggling with a left join in a nHibernate query