3

我有一个UserEntity映射像并得到 不能同时取多个行李。错误流利nhibernate地图多个表

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

我想,当我创建userentity查询来获取两个地址和角色。 我应该怎么做才能看到像

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=? 

回答

2

的输出有没有办法如何产生这样的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); 
} 

这将允许查询根实体与只有几选取[取得也是他们收藏(没有1 + N期)

另请检查What is the solution for the N+1 issue in hibernate?

+0

感谢Radim。我应该避免在映射上使用Join吗?在查询UserEntity时使用JoinAlias是否更好?像你的GetCats()例子,当我有用户列表我会查询孩子也不会我吗? – eyildiz

+0

我的做法是 - 1)加入'多对一'(星型模式),使用投影(例如http://stackoverflow.com/q/24246277/1679310)2)**从不**加入'到many'。使用子查询来过滤集合(例如http://stackoverflow.com/q/23772548/1679310)并让NHibernate分批加载它们... –