views:

38

answers:

1

This is my class:

public class User
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<UserFriend> Friends { get; protected set; }        
}

public class UserFriend
{
    public virtual int Id { get; set; }
    public virtual User User { get; set; }
    public virtual User Friend { get; set; }
}

This is my mapping (Fluent NHibernate):

public class UserMap : ClassMap<User>
{
  public UserMap()
  {
    Id(x => x.Id, "UserId").GeneratedBy.Identity();     
    HasMany<UserFriend>(x => x.Friends);                
  }
}

public class UserFriendMap : ClassMap<UserFriend>
{
    public UserFriendMap()
    {
        Id(x => x.Id, "UserFriendId").GeneratedBy.Identity();

        References<User>(x => x.User).TheColumnNameIs("UserId").CanNotBeNull();
        References<User>(x => x.Friend).TheColumnNameIs("FriendId").CanNotBeNull();
    }
}

The problem is when I execute this code:

User user = repository.Load(1);
User friend = repository.Load(2);

UserFriend userFriend = new UserFriend();
userFriend.User = user;
userFriend.Friend = friend;

friendRepository.Save(userFriend);

var friends = user.Friends;

At the last line, NHibernate generate this query for me:

SELECT
  friends0_.UserId as UserId1_,
  friends0_.UserFriendId as UserFrie1_1_,
  friends0_.UserFriendId as UserFrie1_6_0_, 
  friends0_.FriendId as FriendId6_0_, 
  friends0_.UserId as UserId6_0_ 
  FROM "UserFriend" friends0_ WHERE friends0_.UserId=@p0; @p0 = '1'

QUESTION: Why the query look very wired? It should select only 3 fields (which are UserFriendId, UserId, FriendId) Am I right? or there is something going on inside NHibernate?

A: 

You should have a look at the mapping generated by fluent-nhibernate, maybe that generated something weird.

sirrocco