NHibernate2 request connects when retrieving collection from proxy. Is this the correct behavior?

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;

      

On the last line, NHibernate generates 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 does the request look very wired? He must choose only 3 fields ( UserFriendId

, UserId

, FriendId

) Am I right? or is something going on inside NHibernate?

0


source to share


1 answer


You should take a look at the mapping generated with fluent-nhibernate, it is possible that something strange worked.



0


source







All Articles