SetFetchMode 调用被忽略 [英] SetFetchMode call ignored

查看:21
本文介绍了SetFetchMode 调用被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下查询中的 Criteria API 中调用 SetFetchMode 时遇到问题:

I have a problem with SetFetchMode call in Criteria API in following query:

DetachedCriteria.For<User>()
                .Add<User>(u => u.Status == UserStatus.Live)
                .CreateAlias("UniqueId", "uid")
                .CreateAlias("Companies", "comp")
                .Add(Restrictions.Disjunction()
                                 .Add(Restrictions.Like("uid.Uid", context.Text, MatchMode.Anywhere))
                                 .Add(Restrictions.Like("comp.Name", context.Text, MatchMode.Anywhere)))
                .SetFetchMode("Companies", FetchMode.Eager));

我的课程:

public class User : EntityBase<int>
{
    public virtual UniqueId UniqueId { get; set; }

    public virtual ISet<Company> Companies { get; set; }
}

public class Company : EntityBase<int>
{
    public virtual string Name { get; set; }
}

public class UniqueId : EntityBase<int>
{
    public virtual string Uid { get; set; }
}

和映射

public sealed class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("users");

        Id(x => x.Id).GeneratedBy.Native().Column("id");

        References(x => x.UniqueId).Column("int_unique_id_ref");

        HasMany(x => x.Companies)
            .KeyColumn("user_id")
            .Inverse()
            .AsSet();
    }
}

public sealed class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Table("company");

        Id(x => x.Id).GeneratedBy.Native().Column("id");

        Map(x => x.Name).Column("name");
    }
}

public sealed class UniqueIdMap : ClassMap<UniqueId>
{
    public UniqueIdMap()
    {
        Table("tbl_trading_partner_unique_id");

        Id(x => x.Id).GeneratedBy.Native().Column("int_id");

        Map(x => x.Uid).Column("str_unique_id");
    }
}

但是在获取用户列表后,Nhibernate 再次查询数据库以再次为每个用户获取公司集合.NHibernate 只是忽略了 SetFetchMode 的调用,因为我试图写这样的东西:

But after getting users list Nhibernate is quering data base again to get companies collection for each user once again. NHibernate just ignores call of SetFetchMode, because I have tried to write something like this:

.SetFetchMode("NotExistingProp", FetchMode.Eager)

Nhibernate 没有任何异常.

Nhibernate doesn't thows any exceptions.

我也尝试在映射中将 Lazy 设置为 false,但这也无济于事.不知道如何解决它,有人可以

I also have tried to set Lazy to false in the mappings, but it also didn't help. Have no idea how to fix it, can somebody

然后 Nhibernate 加载了带有实体的集合.但是他还是忽略了SetFetchMode,我可以在那里写任何东西.

and after that Nhibernate loaded collection with entities. But he is still ignoring SetFetchMode, I can write anything there.

推荐答案

解决方案并不那么明显.我们改变了

The solution is not so obvious. We have changed

.CreateAlias("Companies", "comp")

.CreateAlias("Companies", "comp", JoinType.LeftOuterJoin)

这篇关于SetFetchMode 调用被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆