【问题标题】:Fluent nhibernate map multiple tables流畅的nhibernate映射多个表
【发布时间】:2016-01-17 17:27:16
【问题描述】:

我有一个 UserEntity 映射喜欢并得到 无法同时提取多个包。错误

public UserEntityMap : ClassMap<UserEntity>
{
   //Other properties
   HasMany(x => x.Addresses).KeyColumn("User_id").Fetch.Join();
   HasMany(x => x.Roles).KeyColumn("User_id").Fetch.Join();
}

当我为用户实体创建查询时,我想同时获取地址和角色。 我应该怎么做才能看到像

这样的输出
Select * from UserEntity u 
  join Addresses a on u.id=a.User_id 
  join Roles r on u.id=r.User_id where u.id=?

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping


    【解决方案1】:

    没有办法生成这样的 SELECT 语句。

    我建议使用批量提取。有关详细信息,请参阅这些:

    调整后的映射:

    public UserEntityMap : ClassMap<UserEntity>
    {
       //Other properties
       HasMany(x => x.Addresses)
           .KeyColumn("User_id").Fetch.Join()
           .BatchSize(100);
       HasMany(x => x.Roles)
           .KeyColumn("User_id").Fetch.Join()
           .BatchSize(100);
    }
    

    这将允许查询根实体,并且只需几个 SELECTS 即可获得它们的集合(没有 1 + N 问题)

    同时检查What is the solution for the N+1 issue in hibernate?

    【讨论】:

    • 感谢 Radim。我应该避免在映射上使用 Join 吗?查询 UserEntity 时使用 JoinAlias 是否更好?就像您的 GetCats() 示例一样,当我有用户列表时,我会查询孩子,我也不会?
    • 我的方法是 - 1)加入many-to-one(星型模式),使用投影(例如stackoverflow.com/q/24246277/1679310)2)从不加入one-to-many。使用子查询过滤集合(例如stackoverflow.com/q/23772548/1679310)并让NHibernate分批加载它们......
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2010-11-22
    • 1970-01-01
    • 2011-07-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多