nhibernate在2台不同的机器上为同一个源生成2个不同的查询

时间:2011-05-12 13:34:06

标签: .net sql nhibernate criteria

您好
我有两个不同的机器上运行相同的代码和相同的hbms,每个机器都有nhibernate 3(一切都在源存储库上,最新的版本从那里检出)和nhibernate在每台机器上生成不同的sql查询

SearchResult<Contact> searchResult = new SearchResult<Contact>();
            RepositoryLink<Contact> link = new RepositoryLink<Contact>();


                ICriteria icriteria = null;
                var clientCriteria = DetachedCriteria.For<Contact>("c")
                                        .CreateCriteria("ContactRoles","cr", NHibernate.SqlCommand.JoinType.InnerJoin)
                                        .CreateCriteria("AclRole", "ar" ,NHibernate.SqlCommand.JoinType.InnerJoin)
                                        .Add(Restrictions.Eq("ar.RoleName", RoleCode.Client));


            foreach (var keyValuePair in searchCriteria)
            {
                switch (keyValuePair.Key)
                {
                    case SearchClientCriterion.Branch:
                        clientCriteria.CreateCriteria("c.BuEntries", "be" , NHibernate.SqlCommand.JoinType.InnerJoin)
                            .CreateCriteria("be.BuLevel","bl", NHibernate.SqlCommand.JoinType.InnerJoin)
                            .Add(Restrictions.Eq("bl.LevelNo", keyValuePair.Value));
                        break;
                    case SearchClientCriterion.City:
                        clientCriteria.Add(Restrictions.Eq("c.City", keyValuePair.Value));
                        break;
                    case SearchClientCriterion.ClientID:
                        clientCriteria.Add(Restrictions.Eq("c.ID", keyValuePair.Value));
                        break;
                    case SearchClientCriterion.DOB:
                        break;
                    case SearchClientCriterion.FirstName:
                        clientCriteria.Add(Restrictions.Like("c.FirstName", keyValuePair.Value,MatchMode.Anywhere));
                        break;
                    case SearchClientCriterion.LastName:
                        clientCriteria.Add(Restrictions.Like("c.LastName", keyValuePair.Value, MatchMode.Anywhere));
                        break;
                    case SearchClientCriterion.State:
                        clientCriteria.Add(Restrictions.Eq("c.StateId", keyValuePair.Value));
                        break;
                }
            }

            if(buLevel > 0)
                clientCriteria.CreateCriteria("c.BuEntries", "be", NHibernate.SqlCommand.JoinType.InnerJoin)
                        .CreateCriteria("be.BuLevel", "bl", NHibernate.SqlCommand.JoinType.InnerJoin)
                        .Add(Restrictions.Eq("bl.LevelNo", buLevel));


            var clientCountCriteria = (ICriteria)clientCriteria.GetExecutableCriteria(link.Session).Clone();
            searchResult.Count = clientCountCriteria.SetProjection(Projections.CountDistinct("c.ID")).UniqueResult<int>();


            icriteria = clientCriteria.GetExecutableCriteria(link.Session);
            searchResult.Data = icriteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List<Contact>();

这是标准 这会产生:

SELECT count(distinct this_.contactkey) as y0_
FROM   [Contact] this_
       inner join [ContactRole] cr1_
         on this_.contactkey = cr1_.contactkey
       inner join [AclRole] ar2_
         on cr1_.rolekey = ar2_.rolekey
WHERE  ar2_.rolename = 'C' /* @p0 */
       and this_.firstname like '%alex%' /* @p1 */

在一台机器上

SELECT count(distinct this_.contactkey) as y0_
FROM   [Contact] this_
       left outer join [ContactRole] contactrol1_
         on this_.contactkey = contactrol1_.contactkey
       left outer join [AclRole] aclrole2_
         on contactrol1_.rolekey = aclrole2_.rolekey
WHERE  this_.username like '%alex%' /* @p0 */
       and not (aclrole2_.rolename = 'C' /* @p1 */)

有谁知道这是为什么?

2 个答案:

答案 0 :(得分:0)

您在配置中没有任何区别: 差异

            <property name="use_outer_join">true</property>
会话工厂中的

可能是您发出问题的原因。

答案 1 :(得分:0)

最后,我重新创建了整个数据库。删除了一切。重新创建它。 再次检查所有内容,现在它已经有效了。