Nhibernate,我可以改进我的MAPPING或查询

时间:2010-04-06 23:23:16

标签: nhibernate

采用这个简单的例子

引用职员类其他实例的职员类

public class Staff
{
    public Staff()
    {
        Team = new List<Staff>();
    }

    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Staff> Team { get; set; }
    public virtual Staff Manager { get; set; }
}

Fluent Mapping

public class StaffMap : ClassMap<Staff>
{
    public StaffMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        References(x => x.Manager).Column("ManagerId");
        HasMany(x => x.Team).KeyColumn("ManagerId").Inverse();
    }
}

现在我想运行一个查询,它将加载所有员工并急切加载经理和团队成员。这就是我想出来的

IList<Staff> resutls = 
    session.CreateCriteria<Staff>()
    .SetFetchMode("Team", FetchMode.Eager)
    .SetResultTransformer(Transformers.DistinctRootEntity)
    .List<Staff>();

然而SQL(我想做的)有重复的列,2 team2_.ManagerId和2 team2_.Id

SELECT 
this_.Id as Id0_1_, 
this_.Name as Name0_1_, 
this_.ManagerId as ManagerId0_1_, 
team2_.ManagerId as ManagerId3_, 
team2_.Id as Id3_, 
team2_.Id as Id0_0_, 
team2_.Name as Name0_0_, 
team2_.ManagerId as ManagerId0_0_ 
FROM [SelfRef].[dbo].[Staff]
this_ left outer join 
[SelfRef].[dbo].[Staff] team2_ 
on this_.Id=team2_.ManagerId

问题是,这应该发生吗?

我在查询或地图中做错了吗?

或者它是HHib im使用的功能(版本2.1.0.4000)?

非常感谢提前

1 个答案:

答案 0 :(得分:1)

是的,这是正常的,因为你使用它两次作为外键。它将由您的DBMS优化。我通常忽略生成的查询的SELECT部分​​,因为它们对性能没有影响。