SetFetchMode 调用被忽略

发布于 2024-09-27 06:55:12 字数 2209 浏览 6 评论 0原文

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

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 的调用,因为我尝试编写如下内容:

.SetFetchMode("NotExistingProp", FetchMode.Eager)

Nhibernate 不会抛出任何异常。

我还尝试在映射中将 Lazy 设置为 false,但它也没有帮助。 不知道如何修复它,有人可以

在 Nhibernate 加载实体集合后进行修复吗?但他仍然忽略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));

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2024-10-04 06:55:12

解决方案并不是那么明显。我们已更改

.CreateAlias("Companies", "comp")

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

The solution is not so obvious. We have changed

.CreateAlias("Companies", "comp")

to

.CreateAlias("Companies", "comp", JoinType.LeftOuterJoin)
乱了心跳 2024-10-04 06:55:12

由于您正在使用 .CreateAlias("Companies", "comp"),NHibernate 无法预先加载该集合,因为您可能对该别名使用了限制。

我的总体建议是永远不要在集合上使用急切加载。使用 batch_size这个技巧

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文