获取ManyToMany关系以进行加载 [英] Getting ManyToMany relationship to load

查看:106
本文介绍了获取ManyToMany关系以进行加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个User对象,其中包含它所属的组的列表:

I have User object with a list of groups that it belongs to:

public UserHeaderMap()
{
    Table("USER_HEADER");
    Id(x => x.Id, "USER_ID");

    HasManyToMany(x => x.Groups)
        .Table("USER_GROUP_COMPOSITE")
        .ParentKeyColumn("USER_ID")
        .ChildKeyColumn("GROUP_ID")
        .Cascade.SaveUpdate()
        .Inverse();
}

如何在检索时修改映射或检索用户对象以填充组"列表的方式?我确定这里有不同的选择,但是我不确定哪个是最好的.每当我从数据库中检索User对象时,该集合当前为null.我使用以下方法检索它:

How do I need to modify my mapping or the way I'm retrieving my user object to fill the Groups list upon retrieval? I'm sure there are different options here but I'm not sure which is the best. The collection is currently null whenever I retrieve a User object from the database. I retrieve it using this:

UserHeader userFound = session.Load<UserHeader>(newUser.Id);

public class UserHeader
{
    public virtual Guid Id { get; set; }
    public virtual IList<GroupHeader> Groups { get; set; }

    public UserHeader(IList<GroupHeader> groups)
    {
        Groups = groups;
    }

    public UserHeader()
    {
        Groups = new List<GroupHeader>();
    }

    public override bool Equals(object obj)
    {
        bool retVal = false;
        if (obj is UserHeader)
        {
            UserHeader otherUser = (UserHeader)obj;

            if (Id == otherUser.Id)
                retVal = true;
        }

        return retVal;
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
}

Edit2: 这就是我最初查询数据的方式.除了多对多关系之外,它正在抓住一切.

This is the way I was originally querying the data. It's grabbing everything except the many to many relationship.

UserHeader userFound = session.CreateCriteria<UserHeader>()
                              .Add(Example.Create(newUser))
                              .UniqueResult<UserHeader>();

Edit3:单元测试 以下单元测试在foreach(userFound.Groups中的GroupHeader组)上失败.从SQL可以清楚地看到,它正在SQL输出中创建用户和组之间的关系.如有必要,我可以发布.

Unit test The following unit test fails on foreach (GroupHeader group in userFound.Groups). I can clearly see from the SQL that it is creating the relationship between User and Group in the SQL output. I can post it if necessary.

[TestMethod]
public void CanAddUserToGroup()
{
    using (NHibernate.ISession session = SessionOrigin.Current.GetSession())
    {
        using (NHibernate.ITransaction tran = session.BeginTransaction())
        {
            session.SaveOrUpdate(newUser);
            session.SaveOrUpdate(newGroup);
            tran.Commit();
        }

        newGroup.AddUser(newUser);
        using (NHibernate.ITransaction tran = session.BeginTransaction())
        {
            session.SaveOrUpdate(newGroup);
            tran.Commit();
        }

        GroupHeader groupFound = session.CreateCriteria<GroupHeader>()
            .Add(Example.Create(newGroup))
            .UniqueResult<GroupHeader>();

        UserHeader userFound = session.CreateCriteria<UserHeader>()
            .Add(Example.Create(newUser))
            .UniqueResult<UserHeader>();

        UserHeader userFound2 = session.Load<UserHeader>(newUser.Id);

        Assert.IsNotNull(groupFound, "Failed to find group after insertion");
        Assert.IsNotNull(userFound, "Failed to find user after insertion");

        UserHeader userInGroup = null;
        GroupHeader groupInUser = null;

        foreach (UserHeader user in groupFound.Users)
        {
            if (user.Equals(newUser))
                userInGroup = user;
        }

        foreach (GroupHeader group in userFound.Groups)
        {
            if (group.Equals(newGroup))
                groupInUser = group;
        }

        Assert.IsNotNull(userInGroup, "Failed to add a new user to group");
        Assert.IsNotNull(groupInUser, "Failed to add a new group to a user");

        using (NHibernate.ITransaction tran = session.BeginTransaction())
        {
            session.Delete(newUser);
            session.Delete(newGroup);
            tran.Commit();
        }
    }
}

推荐答案

您的单元测试代码不正确.

Your unit test code is not correct.

由于组和用户都已经在会话中加载,因此条件查询将返回相同的实例.

Since both the group and the user are already loaded in the session, the criteria queries return the same instances.

如果集合在内存中为空,则查询后它们仍为空.另外,您只添加了双向关联的一侧.

If the collections were null in memory, they'll still be null after the query. Also, you're adding to just one side of a bidirectional association.

最后但并非最不重要的一点是,您在同一测试方法中测试了太多无关的东西.

Last but not least, you are testing too many unrelated things in the same test method.

这篇关于获取ManyToMany关系以进行加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆