简体   繁体   English

即使lazy = false,Session.Get也会加载N + 1

[英]Session.Get loads with N+1 even when lazy=false

I have entities like these: 我有这样的实体:

public class User
{
     public virtual int Id { get; set;}
    public virtual Iesi.Collections.Generic.ISet<Character> Characters { get; set; }
}

public class Character
{
     public virtual int Id { get; set;}
     public virtual User User { get; set;}
    public virtual Iesi.Collections.Generic.ISet<UserCharacterSmartChallengeTracker> SmartChallengeTrackers { get; set; }
}

public class UserCharacterSmartChallengeTracker
{
    public virtual int Id { get; set; }
    public virtual int? CharacterId { get; set; }
}

They all are NOT lazy loaded. 它们都不是延迟加载的。

When I do session.Get<User>() I see such queries: 当我执行session.Get<User>()我看到这样的查询:

SELECT smartchall0_.character_id                   as character3_1_,
       smartchall0_.id                             as id1_,
       smartchall0_.id                             as id51_0_,
       smartchall0_.character_id                   as character3_51_0_,
FROM   public.user_character_smart_challenge_trackers smartchall0_
WHERE  smartchall0_.character_id = 48176 /* :p0 */

SELECT smartchall0_.character_id                   as character3_1_,
       smartchall0_.id                             as id1_,
       smartchall0_.id                             as id51_0_,
       smartchall0_.character_id                   as character3_51_0_,
FROM   public.user_character_smart_challenge_trackers smartchall0_
WHERE  smartchall0_.character_id = 48175 /* :p0 */

-- and others

I tried to preload all of them to the session cache: 我试图将它们全部预加载到会话缓存中:

 var ids = session.Query<Character>().Where(x => x.User.Id == id)
            .Select(x => x.Id)
            .ToArray();
 session.Query<UserCharacterSmartChallengeTracker>().Where(x => ids.Contains(x.Id)).ToArray();

with query 与查询

select character0_.id as col_0_0_
from   public.characters character0_
where  character0_.user_id = 9602 /* :p0 */
select usercharac0_.id                             as id51_,
       usercharac0_.character_id                   as character3_5
from   public.user_character_smart_challenge_trackers usercharac0_
where  usercharac0_.id in (48176 /* :p0 */, 48175 /* :p1 */, 48174 /* :p2 */, 48173 /* :p3 */,
                           48172 /* :p4 */, 48171 /* :p5 */, 48170 /* :p6 */, 48169 /* :p7 */)

but NHibernate ignores the fact that they all are already loaded into the session cache and generates the same N+1 queries! 但是NHibernate忽略了它们都已经加载到会话缓存中并生成相同的N + 1查询的事实! How to fix this? 如何解决这个问题?


Update: preloading with 更新:预加载

session.QueryOver<Character>().Where(x => x.User.Id == id)
                .Fetch(x => x.User).Lazy
                .Fetch(x=>x.SmartChallengeTrackers).Eager
                .List();

removes N+1 but makes NHibernate load characters second time when I do session.Get<User> which I want to avoid! 删除N + 1,但是在执行session.Get<User>时第二次使NHibernate加载字符,我想避免!

I used futures: 我用期货:

        var q = session.Query<User>().Where(x => x.Id == id);

        var lst = new List<IEnumerable>
        {
            q.FetchMany(x => x.Characters).ToFuture(),
            q.Fetch(x=>x.UpdateableData).ToFuture(),
            session.QueryOver<User>().Where(x => x.Id == id)
                .Fetch(x=>x.Characters).Eager
                .Fetch(x => x.Characters.First().SmartChallengeTrackers).Eager
                .Future()
        };

        var r = session.QueryOver<User>().Where(x => x.Id == id)
            .TransformUsing(Transformers.DistinctRootEntity)
            .Future();

        foreach (IEnumerable el in lst)
        {
            foreach (object o in el)
            {

            }
        }

        return r.ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM