SetFetchMode call ignored

微笑、不失礼 提交于 2019-11-30 09:24:54

问题


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));

My Classes:

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; }
}

And mappings

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");
    }
}

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 doesn't thows any exceptions.

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

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


回答1:


The solution is not so obvious. We have changed

.CreateAlias("Companies", "comp")

to

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



回答2:


Since you are using .CreateAlias("Companies", "comp"), NHibernate can't eager-load the collection, as you could be using restrictions on that alias.

My overall suggestion is to never use eager load on collections. Use batch_size or this trick instead.



来源:https://stackoverflow.com/questions/3889402/setfetchmode-call-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!