开发者

Nhibernate, can i improve my MAPPING or Query

take this simple example

A staff class which references other instances of the staff class

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; }
}

The 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();
    }
}

Now I want to run a query, which will load all the Staff and eager load the manager and Team members. This开发者_运维技巧 is what I came up with

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

however the SQL (does what i want) has duplicate columns, 2 team2_.ManagerId and 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

The question is, should this be happening?

did i do something wrong in the query or map?

or is it a feature of the HHib im using (which is Version 2.1.0.4000)?

many thanks in advance


Yes, it's normal, since you use it twice as a foreign key. It will be optimized by your DBMS. I usually ignore the SELECT part of generated queries, since they have no effect on performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜