0

这是我的课:

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

这是我的映射(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();
    }
}

问题是当我执行此代码时:

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;

在最后一行,NHibernate 为我生成了这个查询:

选择
  Friends0_.UserId 为 UserId1_,
  Friends0_.UserFriendId 为 UserFrie1_1_,
  Friends0_.UserFriendId 为 UserFrie1_6_0_,
  friends0_.FriendId 为 FriendId6_0_,
  Friends0_.UserId 为 UserId6_0_
  FROM "UserFriend" friends0_ WHERE friends0_.UserId=@p0; @p0 = '1'

问题:为什么查询看起来很连贯?它应该只选择 3 个字段(分别是UserFriendIdUserIdFriendId)对吗?或者 NHibernate 内部发生了什么?

4

1 回答 1

0

你应该看看 fluent-nhibernate 生成的映射,也许这会产生一些奇怪的东西。

于 2009-01-11T12:51:43.757 回答