Nhibernate, can i improve my MAPPING or Query

Posted by dbones on Stack Overflow See other posts from Stack Overflow or by dbones
Published on 2010-04-06T23:23:16Z Indexed on 2010/04/06 23:43 UTC
Read the original article Hit count: 226

Filed under:

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

© Stack Overflow or respective owner

Related posts about nhibernate