blogjava's web log

          blogjava's web log
          ...

          Nhibernate MultiQuery

          構(gòu)造多個(gè)查詢。效果不錯(cuò)。注意Nhibernate CreateMultiQuery()這個(gè)方法
          public void MultiQueryTest(int var)
          {
             ISession session = null;
             session = Class1.GetFactory().OpenSession();
             IList result = session.CreateMultiQuery().Add("from Archives where id>:id")
                .Add("select count(*) from Archives a where id>:id").SetInt32("id", var).List();
             //獲得第一個(gè)查詢結(jié)果
             IList archives = (IList)result[0];
             long count = (long)((IList)result[1])[0];
          
             //當(dāng)然也可以session.CreateQuery 構(gòu)造查詢
              //IList result = session.CreateMultiQuery()
               //    .Add(session.CreateQuery("from Archives where id>:id").SetFirstResult(5).SetMaxResults(10))
              //    .Add(session.CreateQuery("select count(*) from Archives a where id>:id"))
              //    .SetCacheable(true).SetInt32("id", var).List();
              // IList archives = (IList)result[0];
             //long count = (long)((IList)result[1])[0];
          
           }
           
          //helper 
          public class Class1
              {
                  private static ISessionFactory factory;
                  private static string _mappingAssembly;
           
                  static Class1()
                  {
           
                      _mappingAssembly = ConfigurationManager.AppSettings["EntityAssemblyName"];
                      NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
                      foreach (string assembly in _mappingAssembly.Split(','))
                      {
                          try
                          {
                              cfg.AddAssembly(assembly);
                          }
                          catch (MappingException me)
                          {
                              throw new MappingException("Assembley Could Not Be Loaded: " + assembly, me);
                          }
                      }
           
                      factory = cfg.BuildSessionFactory();
                  }
           
                  public static ISessionFactory GetFactory()
                  {
                      return factory;
                  }
               }
          更多精彩關(guān)注 http://www.ayende.com/

          posted on 2007-05-12 21:18 record java and net 閱讀(841) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動(dòng)態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 湄潭县| 岢岚县| 金堂县| 宁陵县| 定安县| 鄂州市| 海城市| 德阳市| 锦州市| 驻马店市| 桐庐县| 尼勒克县| 滨海县| 嘉兴市| 卢湾区| 宁城县| 平江县| 竹山县| 永丰县| 海宁市| 平度市| 新巴尔虎右旗| 赤峰市| 墨玉县| 湖南省| 马鞍山市| 南投市| 大英县| 闽清县| 墨脱县| 咸宁市| 竹北市| 诸暨市| 麟游县| 正镶白旗| 南阳市| 安龙县| 巴青县| 太湖县| 应城市| 防城港市|