blogjava's web log

          blogjava's web log
          ...

          Nhibernate MultiQuery

          構造多個查詢。效果不錯。注意Nhibernate CreateMultiQuery()這個方法
          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();
             //獲得第一個查詢結果
             IList archives = (IList)result[0];
             long count = (long)((IList)result[1])[0];
          
             //當然也可以session.CreateQuery 構造查詢
              //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;
                  }
               }
          更多精彩關注 http://www.ayende.com/

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


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 苏尼特左旗| 砚山县| 丰都县| 吉水县| 崇义县| 搜索| 曲阳县| 昆明市| 玉环县| 攀枝花市| 九龙县| 涿鹿县| 睢宁县| 肇庆市| 龙门县| 东海县| 永胜县| 潞西市| 白水县| 吴江市| 盘山县| 历史| 新巴尔虎右旗| 彰武县| 临城县| 礼泉县| 贵定县| 南平市| 通河县| 缙云县| 吕梁市| 宁强县| 临邑县| 新河县| 临高县| 桓仁| 扶绥县| 策勒县| 邳州市| 松滋市| 伊通|