一对多引用

时间:2018-02-07 09:21:36

标签: .net nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

在我的项目中,我遇到了一对多参考的问题。

实际上我引用了三个dto来创建多对多引用:

  • 用户一对多 RoleGroup
  • 角色一对多 RoleGroup

用户可以拥有多个角色,因此可以将角色分配给更多用户。

问题: 当我尝试按标准检索用户时(在Fluent NHibernate中通过示例搜索)我发现重复的行(实际上我在我的数据库中有1个用户用于测试2个角色,当我搜索此用户时,我看到2个相同的行)。

但是如果我尝试通过其键检索用户,我发现了一行(这是正确的)。

下面你可以阅读User和RoleGroup的映射类,然后阅读用于搜索的方法......你能帮我找到问题并给我一些建议吗?

非常感谢大家! NAK

映射:

//USER MAPPING
public class UserMap : ClassMap<User>
{
    public UserMap
    {
        this.Schema("[dbo]");
        this.Table("[users]");

        this.CompositeId(x => x.Key)
            .KeyProperty(x => x.ApplicationId)
            .KeyProperty(x => x.Id);

        this.Map(x => x.Email);
        this.Map(x => x.Password);
        this.Map(x => x.Pin);
        this.Map(x => x.FirstName);
        this.Map(x => x.SecondName);
        this.Map(x => x.IsActive);
        this.Map(x => x.Expiring);
        this.Map(x => x.DateOfBirth);
        this.Map(x => x.Phone);

        this.HasMany(x => x.RoleGroups)
            .Not.LazyLoad()
            .Cascade.AllDeleteOrphan()
            .Fetch.Join()
            .Inverse()
            .KeyColumns.Add("applicationId", "userId");
    }
}

//ROLE GROUP MAPPING
public class RoleGroupMap: ClassMap<RoleGroup>
{
    public RoleGroupMap
    {
        this.Schema("[dbo]");
        this.Table("[role_groups]");

        this.CompositeId(x => x.Key)
            .KeyProperty(x => x.ApplicationId)
            .KeyProperty(x => x.Id);

        this.Map(x => x.UserId);
        this.Map(x => x.RoleId);

        this.References(x => x.User)
            .Not.LazyLoad()
            .Not.Nullable()
            .Cascade.SaveUpdate()
            .Columns("applicationId", "userId");

        this.References(x => x.Role)
            .Not.LazyLoad()
            .Not.Nullable()
            .Cascade.SaveUpdate()
            .Columns("applicationId", "roleId");
    }
}



//FLUENT NHIBERNATE REPO
public class Repository<TIdentifier> : IRepository<TIdentifier> where TIdentifie : class, new()
{

    ...

    //method used to search by criteria, in this particular case Item is type of User and it is used as criteria
    public virtual IEnumerable<TIdentifier> LoadByCriteria(TIdentifier Item, int? RecordStartIndex = null, int? TotalRecords = null)
    {
        IList<Tidentifier> itemList;

        using(ISession session = NHibernateConfiguration<TIdentifier>.OpenSession())
        {
            ICriteria criteria = session.CreateCriteria<TIdentifier>();

            if(RecordStartIndex != null)
            {
                criteria.SetFirstResult((int)RecordStartIndex);
            }

            if(TotalRecords != null)
            {
                criteria.SetMaxResults((int)TotalRecords);
            }

            itemList = criteria.Add(Example.Create(Item))
                .List<TIdentifier>();
        }

        if(itemList == null)
            itemList = new List<TIdentifier>();

        return itemList.ToList();
    }
}

1 个答案:

答案 0 :(得分:0)

避免使用.Fetch.Join()。它在SQL中创建连接,导致行的倍增。要避免N + 1问题,请改用批量提取。

另见: